我有以下内容:
- 产品 POCO
- 仅适用于 EF 代码 (CTP3) 的 ProductConfiguration 类
- 数据库中的产品表
- 数据库中的 ProductCrossSell 表
我的 Product 和 ProductConfiguration 类看起来像:
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Product> CrossSells { get; set; }
}
public class ProductConfiguration : EntityConfiguration<Product>
{
public ProductConfiguration()
{
HasKey(p => p.Id);
Property(p => p.Name).IsRequired().IsUnicode().IsVariableLength().HasMaxLength(254);
// Relationship??
MapSingleType(p => new
{
p.Id,
p.Name
}).ToTable("Product");
}
}
我的数据库表看起来像
CREATE TABLE [dbo].[Product](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](254) NOT NULL
CREATE TABLE [dbo].[ProductCrossSell](
[Id] [uniqueidentifier] NOT NULL,
[ProductId] [uniqueidentifier] NOT NULL
这只是与自身具有多对多关系的 Product 表,其中产品可以链接到其他产品。
有谁知道如何仅在 EF 代码中设置这种关系?我看到在代码中设置其他关系的示例,但没有关于如何设置的示例。
当成功执行 GetProductById 查询时,EF 将连接两个表并填充 Product 对象以及 CrossSells IColletion。
任何帮助表示赞赏。
谢谢