EF6:代码优先复杂类型

时间:2014-02-23 04:38:33

标签: c# entity-framework sql-server-ce domain-driven-design

我无法使用实体框架将我的域实体类与Value Objects(复杂类型)字段展平为一个表。

如果我告诉模型构建器忽略我的值对象/复杂类型,那么一切都有效,但这会导致我的表中遗漏了值对象的所有属性。一旦我删除了忽略语句,我就会得到“跨多个实体共享的值在多个位置创建”。如果我查看生成的CE SQL文件,我会看到一个以我的Domain类命名的附加表,该表附加了1并且只包含Value Object参数。

一些代码:

我的域名类:

public User {

    private User(){}
    public long Id {get; private set;} // dont ask, inherited legacy database
    public string UserId { get; private set; }
    public string Domain { get; private set; }
    public AuditIformation AuditDetails {get ; private set;}

    //..domain logic etc
}

public AuditInformation : IValueObject {
    public long CreatedByUserId { get; private set; }
    public DateTime CreatedDate { get; private set; }
} 

我的存储库项目(首先是代码)得到了这个:

public partial class myContext : DbContext {

    protected override void OnModelCreating(DbModelBuilder mb) {

        mb.Conventions.Remove<PluralizingTableNameConvention>(); 

        mb.ComplexType<Domain.Model.AuditInformation>();
        mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedDate).HasColumnName("Created_On");
        mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedByUserId).HasColumnName("Created_By");

        //This line lets everything work but doesn't include my 
        //AuditInformation attributes in my User Table.
        mb.Ignore<Domain.Model.AuditInformation>(); // <== I think I need to remove this

        //..

        mb.Entity<User>().Map(a => {
            a.Property(x => x.Id).HasColumnName("Id");
            a.Property(x => x.UserId).HasColumnName("User_Id");
            a.Property(x => x.Domain).HasColumnName("User_Dmain");
            })
        .HasKey(x => x.Id)
        .ToTable("Tbl_User");   //<==Again, dont ask

  }
}

我想得的是一张表格如下:

[TBL_USER] 
ID AS BIGINT,
USER_ID as VARCHAR(MAX),
USER_DMAIN AS VARCHAR(MAX),
CREATED_ON as DATE,
CREATED_BY as BIGINT

但是我得到的只是:

[TBL_USER] 
ID AS BIGINT,
USER_ID as VARCHAR(MAX),
USER_DMAIN AS VARCHAR(MAX),

如果我删除了忽略行,我会得到这个奖励怪胎表

[USER1]  <<==Note, named after the domain class, not the destination table.. 
ID AS BIGINT,
CREATED_ON as DATE,
CREATED_BY as BIGINT
当我尝试使用我的存储库时,

和一大堆错误:

----> System.Data.Entity.Infrastructure.DbUpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
----> System.Data.Entity.Core.UpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
----> System.ArgumentException : An item with the same key has already been added.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

我做了很多搜索但是我找不到任何具体的例子,将我的值对象属性保存到为我的域对象创建的表中。有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:9)

试试这个:

public class AuditInformation
{
    public long CreatedByUserId { get; set; }
    public DateTime CreatedDate { get; set; }
}

public abstract class AuditInfo
{
    public AuditInformation AuditDetails { get; set; }

    public AuditInfo()
    {
        this.AuditDetails = new AuditInformation();
        this.AuditDetails.CreatedByUserId = 0;
        this.AuditDetails.CreatedDate = DateTime.Now;
    }
}

public User : AuditInfo
{
    private User(){}
    public long Id {get; private set;} // dont ask, inherited legacy database
    public string UserId { get; private set; }
    public string Domain { get; private set; }

    //..domain logic etc
}

public partial class myContext : DbContext 
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder mb)
    {
        mb.Conventions.Remove<PluralizingTableNameConvention>();

        mb.ComplexType<Domain.Model.AuditInformation>();
        mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedDate).HasColumnName("Created_On");
        mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedByUserId).HasColumnName("Created_By");


        mb.Entity<Cricketer>().Map(a =>
        {
            a.Property(x => x.Id).HasColumnName("Id");
            a.Property(x => x.UserId).HasColumnName("User_Id");
            a.Property(x => x.Domain).HasColumnName("User_Dmain");
            a.Property(x => x.AuditDetails.CreatedByUserId).HasColumnName("CreatedByUserId");
            a.Property(x => x.AuditDetails.CreatedDate).HasColumnName("CreatedDate");
        })
        .HasKey(x => x.ID)
        .ToTable("Tbl_User");   //<==Again, dont ask
    }
}
相关问题