简体   繁体   English

如何使用ADO.NET实体框架代码优先创建一对多关系?

[英]How to create a one to many relationship using ADO.NET Entity Framework Code First?

I am struggling with an issue and from what I've seen none of the propsed solutions work. 我正在为一个问题而苦苦挣扎,从我看到的结果来看,所有提议的解决方案都没有用。

Basically I have two classes: 基本上我有两节课:

public class Role
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class ActionType
{
    public int ID { get; set;}
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Role> AllowedRoles { get; set; }

    public ActionType()
    {
        this.AllowedRoles = new List<Role>();
    }  
}

I would like for EF to force an association table in the DB, where the FK is used to reference the one to many relation. 我希望EF在数据库中强制使用关联表,而FK用于引用一对多关系。 Somehow code first creates a table for my Role where a column ActionType_ID is added. 代码以某种方式首先为我的Role创建一个表,其中添加了ActionType_ID列。 Not useful of course when more Role s are added to one ActionType . 当将更多Role添加到一个ActionType时,当然没有用。 A DBUpdateExcpetion is being trown. 一个DBUpdateExcpetion被抛出。

Any thoughts on how I should resolve this? 关于如何解决这个问题有任何想法吗? The point being that classe Role doesn't refer to ActionType, since this is not useful. 关键是类Role不引用ActionType,因为这没有用。

Thank you! 谢谢!

What are you really trying to do? 您到底想做什么? At the moment you can add more Role s to single ActionType but single Role cannot be added to multiple ActionType s. 目前,您可以将更多Role添加到单个ActionType但是不能将单个Role添加到多个ActionType Do you want that as well? 你也想要吗?

In such case you are not doing one-to-many association but many-to-many. 在这种情况下,您不是在进行一对多关联,而是进行多对多关联。 Only many-to-many relation uses junction (association) table. 只有多对多关系使用联结(association)表。 One-to-many relation indeed means that dependent entity (the one on many side) has FK to principal entity. 一对多关系的确意味着从属实体(在许多方面都是一个)与主体之间具有FK。 The simplest approach is defining navigation property in Role as well: 最简单的方法也是在Role中定义导航属性:

public class Role
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<ActionType> ActionTypes { get; set; } 
}

public class ActionType
{
    public int ID { get; set;}
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Role> AllowedRoles { get; set; } 
}

If you don't want to have navigation property in Role you must use fluent API to describe the relation. 如果您不想在Role中具有导航属性,则必须使用流畅的API来描述关系。

public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ActionType>()
                    .HasMany(a => a.AllowedRoles)
                    .WithMany();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM