English 中文(简体)
EF 编码第一绘图复杂类型关系
原标题:EF code-first mapping complex type relationship

我对被审计物体有一个基级:

http://www.ohchr.org。

public class AuditableObject : DomainObject, IAuditable
{
    ... some fields

   public AuditInfo AuditInfo
   {
        get;
        set;
   }
}

<>AuditInfo

public class AuditInfo : IAuditable
{

    public int CreatedByDbId
    {
        get;
        set;
    }

    public DateTime CreatedDate
    {
        get;
        set;
    }

    public int? AmendedByDbId
    {
        get;
        set;
    }

    public DateTime? AmendedDate
    {
        get;
        set;
    }
}

The CreatedByDbId and A amendedByDb Id与系统User物体有关:

<>SystemUser

public class SystemUser
{
    public int SystemUserDbId
    {
        get;
        set;
    }

    public string Username
    {
        get;
        set;
    }
}

我有一个等级:<>Call,继承来自可审计物体的遗产,该系统还拥有其他特性:

public class Call : AuditableObject
{
    ... some fields

    public SystemUser TakenBy { get; set;}
    public SystemUser CreatedBy { get; set; }
    public SystemUser CancelledBy { get; set;}
    public int CancelledByDbId {get; set;}
    public int TakenByDbId { get; set;}
}

www.un.org/Depts/DGACM/index_spanish.htm 电话数据库表

CREATE TABLE [dbo].[Call](
[CallDbId] [int] IDENTITY(1,1) NOT NULL,
[CancelledBy] [int] NULL,
[CreatedByDbId] [int] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[AmendedByDbId] [int] NULL,
[AmendedDate] [datetime] NULL,
[TakenBy] [int] NOT NULL)

我似乎无法正确绘制地图,例如。

modelBuilder.ComplexType<AuditInfo>();
...// configuration for Call
this.Property(x => x.AuditInfo.AmendedByDbId).HasColumnName("AmendedByDbId");
this.Property(x => x.AuditInfo.AmendedDate).HasColumnName("AmendedDate");
this.Property(x => x.AuditInfo.CreatedByDbId).HasColumnName("CreatedByDbId");
this.Property(x => x.AuditInfo.CreatedDate).HasColumnName("CreatedDate");

this.Property(t => t.CancelledByDbId).HasColumnName("CancelledBy");
this.Property(t => t.TakenByDbId).HasColumnName("TakenBy");

this.HasRequired(t => t.TakenBy).WithMany().HasForeignKey(x => x.TakenByDbId);
this.HasRequired(t => t.CancelledBy).WithMany().HasForeignKey(x => x.CancelledByDbId);

并且我总是在运行时出现错误,例如:

Invalid column name  SystemUser_SystemUserDbId .
Invalid column name  SystemUser_SystemUserDbId1 .
Invalid column name  SystemUser_SystemUserDbId2 .
Invalid column name  CreatedBy_SystemUserDbId .

我可以 figure一 figure一 figure:

最佳回答

最后一个错误 页: 1 创建By_SystemUserDbId“是因为没有绘制地图。 CreatedBy Navigation property to 数据库中外国关键栏目。 它希望:

this.HasRequired(t => t.CreatedBy)
    .WithMany()
    .Map(c => c.MapKey("CreatedByDbId"));

我不知道目前其他三个错误的原因。

<><>Edit>/strong>

我感到,这一模式确实是困难的,或者也许不可能绘制地图。

  • You have a navigation property CreatedBy on the class Call but the foreign key property CreatedByDbId on a complex type AuditInfo of the base class AuditableObject. I have doubt that you can define in the mapping that these properties belong together in the same relationship to SystemUser.

  • 因此,导航财产<代码>CreatedBy 外国钥匙<代码> 创建ByDbId。 但是,这是不可能的,因为你不能将航行财产置于一种复杂的类型之中。

  • 是否需要为<代码>AuditInfo使用一种复杂的类型? 页: 1 缩略语

    public class AuditableObject : DomainObject, IAuditable
    {
        ... some fields
    
        public int CreatedByDbId { get; set; }
        public DateTime CreatedDate { get; set; }
        public int? AmendedByDbId { get; set; }
        public DateTime? AmendedDate { get; set; }
    }
    
  • 如果你能够这样做,你就能够移动<代码>。 CreatedBy Navigation property from the Call entities to the Auditable 反对: “FKCreatedByDbId的基类,然后就另外两种导航特性绘制了你已经做的同样的地图,但基类的这一次是:

    modelBuilder.Entity<AuditableObject>()
        .HasRequired(a => a.CreatedBy)
        .WithMany()
        .HasForeignKey(a => a.CreatedById);
    
问题回答

暂无回答




相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...