EF Code First:使用中间表将实体映射到现有数据库

发布于 2024-11-01 17:52:44 字数 1713 浏览 1 评论 0原文

这是一个示例场景。我有一个现有的数据库,包含如下表;

订单,带有字段OrderId(PK,int)

产品,带有字段ProductId(PK,int),< em>PriceId (FK, int)

OrdersProducts,包含字段 OrderProductId (PK, int), OrderId (FK, int) , ProductId(FK,int),OrderingStatus(int)

价格,包含字段PriceId(PK,int)

所有PK是身份。

我的实体是;

public class Order
{
 [Key]
 public int OrderId { get; set; }
 public virtual IList<Product> Products { get; set; }
}

public class Product
{
 [Key]
 public int ProductId { get; set; }
 public string Name { get; set; }
 public int OrderingStatus { get; set; }

 public virtual Price Price { get; set;}

}

public class Price
{
 [Key]
 public int PriceId { get; set;}

}

这是我的映射; 命令;

  HasMany<Product>(x => x.Products)
    .WithMany()
    .Map(m =>
    {
      m.MapLeftKey("OrderId");
      m.MapRightKey("ProductId");
      m.ToTable("OrdersProducts", "dbo");
    });

产品;

Map(m =>
  {
    m.Properties(p => new
    {
      p.Name
    });
    m.ToTable("Products", "dbo");
  });

  Map(m =>
  {
    m.Properties(p => new
    {
      p.OrderingStatus
    });

    m.ToTable("OrdersProducts", "dbo");
  });

  HasRequired<Price>(x => x.Price)
    .WithMany()
    .Map(m => m.MapKey("PriceId"));

价格;

  ToTable("Prices", "dbo");

我无法在上下文中正确处理我的映射,有没有人可以帮助我朝正确的方向发展。

我实际上在这里遇到两种麻烦,首先是 OrderingStatus 到中间表的映射,其次是连接表时遇到问题,即“指定的架构无效”。错误:...错误0019:ia 类型中的每个属性名称必须是唯一的。属性名称“OrdersProductsId”已定义。


This is a sample scenario. I have an existing database consisting of tables as below;

Orders, with field OrderId (PK, int)

Products, with fields ProductId (PK, int), PriceId (FK, int)

OrdersProducts, with fields OrderProductId (PK, int), OrderId (FK, int), ProductId (FK, int), OrderingStatus (int)

Price with field PriceId (PK, int)

All PKs are identity.

My Entities are;

public class Order
{
 [Key]
 public int OrderId { get; set; }
 public virtual IList<Product> Products { get; set; }
}

public class Product
{
 [Key]
 public int ProductId { get; set; }
 public string Name { get; set; }
 public int OrderingStatus { get; set; }

 public virtual Price Price { get; set;}

}

public class Price
{
 [Key]
 public int PriceId { get; set;}

}

And here are my mappings;
Order;

  HasMany<Product>(x => x.Products)
    .WithMany()
    .Map(m =>
    {
      m.MapLeftKey("OrderId");
      m.MapRightKey("ProductId");
      m.ToTable("OrdersProducts", "dbo");
    });

Product;

Map(m =>
  {
    m.Properties(p => new
    {
      p.Name
    });
    m.ToTable("Products", "dbo");
  });

  Map(m =>
  {
    m.Properties(p => new
    {
      p.OrderingStatus
    });

    m.ToTable("OrdersProducts", "dbo");
  });

  HasRequired<Price>(x => x.Price)
    .WithMany()
    .Map(m => m.MapKey("PriceId"));

Price;

  ToTable("Prices", "dbo");

I can't get this right with my mappings in the context, is there anyone that can help me in the right direction.

I am actually having two kinds of trouble here, first the mapping of OrderingStatus to my intermediate table, second I have trouble connecting my tables, ie 'Schema specified is not valid. Errors: ... error 0019: Each property name in i a type must be unique. Property name ''OrdersProductsId" was already defined.'


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2024-11-08 17:52:44

您的映射将不起作用 - 您无法将 OrderingStatus 映射到您的 Prodcut,因为它不在同一个表中,并且与产品没有一对一的关系。它位于具有一对多关系的单独表中。您必须将 OrdersProducts 公开为单独的实体,因为您的联结表包含您要使用的其他属性:

public class Order
{
    [Key]
    public int OrderId { get; set; }
    public virtual ICollection<ProductOrder> ProductOrders { get; set; }
}

public calss ProductOrder
{
    [Key]
    public int OrderProductId { get; set; }
    public int OrderStatus { get; set; }
    public virtual Product { get; set; }
    public virtual Order { get; set; }
}

public class Product
{
    [Key]
    public int ProductId { get; set; }
    public string Name { get; set; }
    public virtual Price Price { get; set;}
}

public class Price
{
    [Key]
    public int PriceId { get; set;}
}

Your mapping will not work - you can't map OrderingStatus to your Prodcut because it is not in the same table and it is not related by one-to-one relation to the Product. It is in separate table with one-to-many relation. You must expose OrdersProducts as separate entity because your junction table contains additional properties which you want to use:

public class Order
{
    [Key]
    public int OrderId { get; set; }
    public virtual ICollection<ProductOrder> ProductOrders { get; set; }
}

public calss ProductOrder
{
    [Key]
    public int OrderProductId { get; set; }
    public int OrderStatus { get; set; }
    public virtual Product { get; set; }
    public virtual Order { get; set; }
}

public class Product
{
    [Key]
    public int ProductId { get; set; }
    public string Name { get; set; }
    public virtual Price Price { get; set;}
}

public class Price
{
    [Key]
    public int PriceId { get; set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文