Why don't CreateDatabaseIfNotExists create a relationship although db gets created

最后都变了- 提交于 2020-01-17 03:55:05

问题


I create a db from my edmx file like this:

Database.SetInitializer(new CreateDatabaseIfNotExists<myEntities>());

And the db is created. However, there is one recursive relationship missing. It is in the edmx, but not in the db. Why does it fail to create a recursive relationship and how can I fix it?

By recrusive relationship I mean a table that has a relationship with itself.

In the myEntities class, one of the fileds is this:

public DbSet<Product> Products { get; set; }

And in an auto-generated file:

public partial class Product
{
    public Product()
    {
        this.Product1 = new HashSet<Product>();
        this.Products = new HashSet<Product>();
        this.Ingredients = new HashSet<Ingredient>();
    }

    public int ProductId { get; set; }

    public virtual ICollection<Product> Product1 { get; set; }
    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Ingredient> Ingredients { get; set; }
}

来源:https://stackoverflow.com/questions/27423193/why-dont-createdatabaseifnotexists-create-a-relationship-although-db-gets-creat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!