0

I have three entities, User, Domain and Role.

I have this class to define the relationship of the three entities:

public class UserDomainRole
{
        [Key]
        public int UserId { get; set; }
        [Key]
        public int DomainId { get; set; }
        [Key]
        public int RoleId { get; set; }
}

So how to I setup a foreign key relationship between UserDomainRole and other three table? Is it something like this?

public class DeniedDomainRole
{
    [Key]
    public int UserId { get; set; }
    public virtual User User { get; set; }
    [Key]
    public int DomainId { get; set; }
    public virtual Domain Domain { get; set; }
    [Key]
    public int RoleId { get; set; }
    public virtual Role Role { get; set; }
}

Then entity framework will work the relationship out?

2 Answers 2

0

This code will give you an exception with a message "Unable to determine composite primary key ordering"

In code first if you want to create a composite primary key then you have to use the to give the key/column order, like that:

[Key, Column(Order=1)]

Where

Order = The composite Primary key/column order on the table

public class UserDomainRole
{
        [Key, Column(Order=1)]
        public int UserId { get; set; }

        [Key, Column(Order=2)]
        public int DomainId { get; set; }

        [Key, Column(Order=3)]
        public int RoleId { get; set; }
}

So how to I setup a foreign key relationship between UserDomainRole and other three table? Is it something like this?

It is depending on what kind of the relationships you have:

  • Configure One-to-Zero-or-One Relationship:

Here, we will configure One-to-Zero-or-One relationship between two entities, e.g. Entity1 can be associated with zero or only one instance of Entity2. http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

  • Configure One-to-Zero-or-One Relationship:

Here, we will configure One-to-Zero-or-One relationship between two entities, e.g. Entity1 can be associated with zero or only one instance of Entity2. http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

  • Configure Many-to-Many relationship:

Here, we will learn how to configure Many-to-Many relationship between the Student and Course entity classes. Student can join multiple courses and multiple students can join one course. http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

5
  • Hi Bassam, realised that now, I run the command Add-Migration AddDeniedDomainRole and a c# class is created which include the right migration code. However, when I run my code, it gives me this error: The model backing the 'XXX' context has changed since the database was created. Consider using Code First Migrations to update the database. What can I do to make entity framework to update the tables as the migration code has been generated?
    – daxu
    Jul 1, 2016 at 14:37
  • you have to call first update-database or just delete the db with any tool like ssm Jul 1, 2016 at 14:40
  • ok, I will give a try. Also do you know why in my migration code, the migration code added this line: RenameTable(name: "dbo.DomainRole", newName: "RoleDomain"); I thought it is not needed so I commented out this line in the migration file, however, I got an error when I try to do a query "Invalid object name 'dbo.RoleDomain'". I tried to search my code, everything still uses DomainRole, not RoleDomain?
    – daxu
    Jul 1, 2016 at 14:52
  • write all your entities in the question and the db context Jul 1, 2016 at 14:54
  • return Db.Roles .AsNoTracking() .Where(r => !r.IsDeleted && r.Users.Any(u => u.UserId == userId) && ((productId == 1 && r.Name.Equals("User Admin", StringComparison.InvariantCultureIgnoreCase)) || r.Domains.Any(d => d.DomainId == domainId))) .Select(r => r.Name) .Distinct() .ToList();
    – daxu
    Jul 1, 2016 at 15:00
0

In this way you can relation with the user model

public int? UserUserId{get;set;}
public virtual User User{get;set;}

? makes nullable your foreignkey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.