我使用EF Code First模型在数据库中有更多列

时间:2015-03-16 12:50:45

标签: c# entity-framework ef-code-first

我正在使用.Net Framework 4.5.1和C#开发实体框架代码第6.1.2。

我有这两个表:

public class CODES
{
    public string CODE { get; set; }
    public int BATCH_ID { get; set; }
    public byte CODE_LEVEL { get; set; }
    public byte COMMISIONING_FLAG { get; set; }

    public virtual AGGREGATION_CHILDS AggregationChild { get; set; }
    public virtual List<AGGREGATION_CHILDS> AggregationChildren { get; set; }
    public virtual BATCHES Batch { get; set; }
}
public class AGGREGATION_CHILDS
{
    public string CODE { get; set; }
    public string CODE_CHILD { get; set; }
    public int POSITION { get; set; }

    public virtual CODES Aggregation { get; set; }
    public virtual CODES Code { get; set; }
}

他们的实体框架映射:

class CODESConfiguration : EntityTypeConfiguration<CODES>
{
    public CODESConfiguration()
    {
        HasKey(c => c.CODE);

        Property(c => c.CODE)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        Property(c => c.CODE)
            .HasMaxLength(20);

        Property(c => c.BATCH_ID)
            .IsRequired();

        Property(c => c.CODE_LEVEL)
            .IsRequired();

        Property(c => c.COMMISIONING_FLAG)
            .IsRequired();

        HasRequired(c => c.Batch)
            .WithMany(b => b.Codes)
            .HasForeignKey(c => c.BATCH_ID);
    }
}

class AGGREGATION_CHILDSConfiguration : EntityTypeConfiguration<AGGREGATION_CHILDS>
{
    public AGGREGATION_CHILDSConfiguration()
    {
        HasKey(ag_ch => new { ag_ch.CODE, ag_ch.CODE_CHILD});

        Property(ag_ch => ag_ch.CODE)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        Property(ag_ch => ag_ch.CODE_CHILD)
            .HasMaxLength(20)
            .IsRequired();

        HasRequired(ag_ch => ag_ch.Aggregation)
            .WithOptional(c => c.AggregationChild);

        HasRequired(ag_ch => ag_ch.Code)
            .WithMany(c => c.AggregationChildren)
            .HasForeignKey(ag_ch => ag_ch.CODE_CHILD);
    }
}

我想用这些关系创建这两个表:

CREATE TABLE [dbo].[AGGREGATION_CHILDS]
(
    [CODE] [nvarchar](20) NOT NULL,
    [CODE_CHILD] [nvarchar](20) NOT NULL,
    [POSITION] [int] NOT NULL,
    CONSTRAINT [PK_AGGREGATION_CHILDS] PRIMARY KEY CLUSTERED 
    (
        [CODE] ASC,
        [CODE_CHILD] ASC
    ), 
    CONSTRAINT [FK_AGGREGATION_CHILDS_AGGREGATIONS] FOREIGN KEY ([CODE]) REFERENCES [CODES]([CODE]), 
    CONSTRAINT [FK_AGGREGATION_CHILDS_CODES] FOREIGN KEY ([CODE_CHILD]) REFERENCES [CODES]([CODE])
CREATE TABLE [dbo].[CODES]
(
    [CODE] [nvarchar](20) NOT NULL,
    [BATCH_ID] int NOT NULL,
    [CODE_LEVEL] [tinyint] NOT NULL,
    [COMMISIONING_FLAG] [tinyint] NOT NULL,
     CONSTRAINT [PK_CODES] PRIMARY KEY CLUSTERED 
    (
        [CODE] ASC
    ),
    CONSTRAINT [FK_CODES_BATCHES] FOREIGN KEY ([BATCH_ID]) REFERENCES [dbo].[BATCHES] ([Id])
)

但我得到了这个: enter image description here

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Notmapped属性 EF attribute NotMapped explained

它不会在数据库上创建字段。

一旦声明了外键,就会使用索引字段。 您可以控制外键字段名称 Foreign key attributes 或者您已完成.HasForeignKey(ag_ch => ag_ch.CODE_CHILD);

不要忘记您可以使用EF电动工具对数据库进行反向工程以进行编码。如果你有一个简单的数据库样本,那么。您可以先生成映射到该代码的代码。