首先创建映射代码

时间:2016-05-03 11:34:25

标签: c# entity-framework

好的,这让我疯了。 我曾尝试使用Code First构建一些表,但我不能让它按照我的要求去做。 所以我使用SQL Management studio来创建我现在要发布的表格:

CREATE TABLE [dbo].[Products](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nchar](10) NULL,
 CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

以及我有的产品细节

CREATE TABLE [dbo].[ProductDetail](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nchar](10) NULL,
    [Summary] [nchar](10) NULL,
    [ProductId] [int] NOT NULL,
 CONSTRAINT [PK_ProductDetail] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ProductDetail]  WITH CHECK ADD  CONSTRAINT [FK_ProductDetail_Products] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Products] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[ProductDetail] CHECK CONSTRAINT [FK_ProductDetail_Products]
GO

如您所见,如果删除产品,则 ProductDetails 也会被删除。 但如果我删除 ProductDetails ,则不会级联。

出于某种原因,我很难在Code First EF中复制它。 有人能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我总是使用Entity Framework Power Tools生成我的代码第一个域类和映射,并且效果很好。我已经使用此工具在上面发布的两个表的基础上生成代码优先类。他们在这里:

public partial class Product
{
    public Product()
    {
        this.ProductDetails = new List<ProductDetail>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ProductDetail> ProductDetails { get; set; }
}

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Name)
            .IsFixedLength()
            .HasMaxLength(10);

        // Table & Column Mappings
        this.ToTable("Products");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Name).HasColumnName("Name");
    }
}

public partial class ProductDetail
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

public class ProductDetailMap : EntityTypeConfiguration<ProductDetail>
{
    public ProductDetailMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Title)
            .IsFixedLength()
            .HasMaxLength(10);

        this.Property(t => t.Summary)
            .IsFixedLength()
            .HasMaxLength(10);

        // Table & Column Mappings
        this.ToTable("ProductDetail");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.Summary).HasColumnName("Summary");
        this.Property(t => t.ProductId).HasColumnName("ProductId");

        // Relationships
        this.HasRequired(t => t.Product)
            .WithMany(t => t.ProductDetails)
            .HasForeignKey(d => d.ProductId);

    }
}