实体框架核心。如何将两个集合映射到一列?

时间:2018-01-26 10:39:43

标签: c# entity-framework entity-framework-core

我有以下模型,我想用Entity Framework Core管理:

GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Courier.class, new CourierDeserializer());
mGson = gson.create();
builder.setConverter(new GsonConverter(mGson));

我在这里创建实体:

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
}

public class ProductsReport
{
    public int ID { get; set; }
    public string Author { get; set; }
    public string ReportText { get; set; }

    public List<Product> GoodProducts { get; set; } = new List<Product>();
    public List<Product> BadProducts { get; set; } = new List<Product>();
}

但这会创建以下产品表:

using (ProductsDbContext db = new ProductsDbContext())
        {
            Product p1 = new Product
            {
                Name = "Orange",
                Price = 10
            };

            Product p2 = new Product
            {
                Name = "Lemon",
                Price = 7
            };

            Product p3 = new Product
            {
                Name = "Salt",
                Price = 1
            };

            Product p4 = new Product
            {
                Name = "Sugar",
                Price = 3
            };

            db.Products.AddRange(p1, p2, p3, p4);

            ProductsReport rep1 = new ProductsReport
            {
                Author = "Me",
                ReportText = "I can say, that some prodcuts are bad",
                BadProducts = new List<Product> { p1, p2 },
                GoodProducts = new List<Product> { p3, p4 }
            };

            db.ProductsReports.Add(rep1);

            db.SaveChanges();
        }

对于同一ProductsReport实体,有两个具有外部字符串的列。

有没有办法指定Entity Framework应该只创建一个外键?

0 个答案:

没有答案