Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please how do I assign table name to generic DBSet<t> base on Model?
I want to create a table for every registered user, using their userId and the table name in EF6.

public class ContactModel
    {
        [Key]
        [Required]
        [MaxLength(20), MinLength(6)]
        public string Phone { get; set; }
        [MaxLength(45), MinLength(2)]
        public string Name { get; set; }
        [MaxLength(100)]
        public string Email { get; set; }
        
    }


What I have tried:

[Table("Logedin Userid")]
public class ContactModel
    {
        [Key]
        [Required]
        [MaxLength(20), MinLength(6)]
        public string Phone { get; set; }
        [MaxLength(45), MinLength(2)]
        public string Name { get; set; }
        [MaxLength(100)]
        public string Email { get; set; }
        
    }
Posted
Updated 10-Jun-20 23:19pm

1 solution

That's a terrible idea!

Create a single table for your contacts, and add a foreign key relationship with the users table.
C#
public class User
{
    [Key]
    public Guid Id { get; set; }
    
    public virtual IList<Contact> Contacts { get; set; }
    
    ... Other properties ...
}

public class Contact
{
    [Key]
    public Guid Id { get; set; }
    
    public Guid UserId { get; set; }
    
    public virtual User User { get; set; }
    
    [Required]
    [MaxLength(20), MinLength(6)]
    public string Phone { get; set; }
    
    [MaxLength(45), MinLength(2)]
    public string Name { get; set; }
    
    [MaxLength(100)]
    public string Email { get; set; }
}
Relationships, navigation properties, and foreign keys - EF6 | Microsoft Docs[^]

You'll also want to check your [Key] definition - most systems allow multiple contacts with the same telephone number, even for the same parent entity. If you really want a unique telephone number per user, then create a composite key.
C#
public class Contact
{
    [Key, Column(Order = 0)]
    public Guid UserId { get; set; }
    
    public virtual User User { get; set; }
    
    [Required]
    [MaxLength(20), MinLength(6)]
    [Key, Column(Order = 1)]
    public string Phone { get; set; }
    
    ... Other properties ...
}
Composite Keys - Code First Data Annotations - EF6 | Microsoft Docs[^]
 
Share this answer
 
Comments
Enobong Adahada 12-Jun-20 19:43pm    
Thank you for the reply,
Actually what i want to do is to create a table for every login user using their user id as the table name in the DB, please how do i do that.
Thank again.
Richard Deeming 15-Jun-20 5:11am    
And as I said, that's a terrible idea. Your database will be an absolute mess, and completely unmaintainable.

There's a reason Entity Framework doesn't let you do things like this.

If you absolutely insist on pursuing this terrible idea, you'll need to use raw ADO.NET, and manage the queries yourself.
Enobong Adahada 15-Jun-20 17:59pm    
Thank you so much Richard for your response, I looked into it, its really a bad idea to have multiple tables with the same structure.
Thank you again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900