c# - EF 代码第一个 : Defining foreign keys

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

我看过一些其他答案和很多文章,但这个简单的部分仍然让我困惑。我使用的是 EF Code First 4.1,但如果更新版本更容易的话,我很乐意迁移到该版本。我有一个像这样的主要事实表:

namespace Spend.Models
{
    public class ExpenseItem
    {
        [Key]
        public String UniqueID_ERLineID { get; set; }
        public String ERNum { get; set; }
        public String ItemNum { get; set; }
        public String Parent_Expense_Item { get; set; }
        public String Card_Number { get; set; }
...

以及几个与 ExpenseItems 具有多对一关系的表:

public class ExpenseItemAccounting
{
    [Key]
    public String UniqueID_Accounting { get; set; }
    public String ERLineID { get; set; }
    public String ERNum { get; set; }
    public String ItemNum { get; set; }

正如我们所看到的,第二个表中的 ERLineID 连接到第一个表中的 UniqueID_ErLineID,因此我通常依赖的“约定”不起作用。所以我需要使用虚拟 ICollection - 但我希望它将这些字段指定为链接。任何有关如何执行此操作的帮助表示赞赏。

PS。我目前无法重命名数据库字段。

@卢克:

我应用了你提到的更改,它们很有意义。但是我收到以下错误:

System.Data.Entity.ModelConfiguration.ModelValidationException occurred
  Message=One or more validation errors were detected during model generation:

    System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'ExpenseItemAccounting_ExpenseItem_Target' in relationship 'ExpenseItemAccounting_ExpenseItem'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
    System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'ExpenseItemAccounting_ExpenseItem_Source' in relationship 'ExpenseItemAccounting_ExpenseItem'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be �1�.

  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
       at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
       at System.Linq.Queryable.SelectMany[TSource,TCollection,TResult](IQueryable`1 source, Expression`1 collectionSelector, Expression`1 resultSelector)
       at EmailClient.Prog.getData() in C:\MF\Dropbox\Dev_LN_Projects\04_QA\EmailClient\EmailClient\Prog.cs:line 172
  InnerException: 

这是当我尝试以下 linq 查询时出现的:

        var geee = (from e in db.ExpenseItems
                        from f in db.ExpenseItemFbtItems
                        where
                        e.Item_Transaction_Date.Value.Year == 2011 &&
                        e.Item_Transaction_Date.Value.Month == 8
                        select new { A = e.UniqueID_ERLineID, B = f.ERLineID.First() });

我实际上希望能够说 e.ExpenseItemAccounting.ItemNum 或类似的内容 - 我是否需要在 ExpenseItem 定义中添加一些内容才能实现此目的?

我的模型设置如下。 base.OnModelCreating 通过智能感知出现,我尝试过使用/不使用它以获得相同的结果:

public class SpendDB : DbContext
{
    public DbSet<ExpenseAttachment> ExpenseAttachments {get; set; }
    public DbSet<ExpenseComment> ExpenseComments {get; set; }
    public DbSet<ExpenseItemAccounting> ExpenseAccountings {get; set; }
    public DbSet<ExpenseItemFbtItem> ExpenseItemFbtItems {get; set; }
    public DbSet<ExpenseItem> ExpenseItems {get; set; }
    public DbSet<ExpenseItemViolation> ExpenseItemViolations {get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ExpenseItemAccounting>().HasOptional(e => e.ExpenseItem).WithMany().HasForeignKey(e => e.UniqueID_Accounting);
    }
}

也许我需要将虚拟 ICollection 放入 ExpenseItem 定义中?或者可能是相反的情况 - 比如说 modelBuilder.Entity 有可选的 ExpenseItemAccounting?这对我来说听起来更直观,但我(显然)不太擅长这一点,所以要持保留态度!

再次感谢

最佳答案

这样做:

public class ExpenseItemAccounting
{
    [Key]
    public String UniqueID_Accounting { get; set; }
    public ExpenseItem ExpenseItem{get;set;}
    public String ERLineID { get; set; }
    public String ERNum { get; set; }
    public String ItemNum { get; set; }
}

然后在你的模型构建器中使用

modelBuilder.Entity<ExpenseItemAccounting>()
    .HasOptional(e => e.ExpenseItem).WithMany()
    .HasForeignKey(e => e.UniqueID_Accounting );

编辑:

要将导航属性配置为在另一端有一个集合,您只需像这样添加它

public class ExpenseItem
{
        [Key]
        public String UniqueID_ERLineID { get; set; }
        public String ERNum { get; set; }
        public String ItemNum { get; set; }
        public String Parent_Expense_Item { get; set; }
        public String Card_Number { get; set; }
        public ICollection<ExpenseItemAccounting> ExpenseItemAccountings{ get; set; }
}

然后通过修改模型构建器配置来连接它,如下所示:

 modelBuilder.Entity<ExpenseItemAccounting>()
     .HasOptional(e => e.ExpenseItem).WithMany(e=> e.ExpenseItems)
     .HasForeignKey(e => e.UniqueID_Accounting );

这会将其连接起来,以便 ExpenseItem 将具有所有子 ExpensItemAccounting 的列表,如果更有意义,您还可以添加该版本的单数版本,例如:

 public class ExpenseItem
    {
            [Key]
            public String UniqueID_ERLineID { get; set; }
            public String ERNum { get; set; }
            public String ItemNum { get; set; }
            public String Card_Number { get; set; }
            public ExpenseItemAccounting Parent_Expense_Item { get; set; }
    }

并使用 modelBuilder 进行配置:

modelBuilder.Entity<ExpenseItemAccounting>()
    .HasOptional(e => e.ExpenseItem)
    .WithOptionalDependent(e=>e.Parent_Expense_Item);

我认为如果您还希望连接 FK 引用(而不仅仅是导航属性),您需要在单独的语句中执行此操作,但这有点麻烦。

查看有关导航属性以及如何使用 modelBuilder 的 MSDN 页面,因为它有很多高级内容的好示例。

http://msdn.microsoft.com/en-us/library/hh295843(v=vs.103).aspx

关于c# - EF 代码第一个 : Defining foreign keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612611/

相关文章:

configuration - 有没有办法使用 EF 4.2 codefirst 配置用设置的 maxlength 值覆盖 nvarchar(max) ?

c# - XML 文档注释中的 XML (C#)

performance - Entity Framework 查询速度

c# - 通过 SOAP 的 MVC 3 存储库模式

entity-framework - Entity Framework 标量函数映射

azure - 使用远程连接字符串在 Azure 上执行代码优先迁移

database - 在 EF 迁移期间更改数据库选项

c# - 属性的 MinValue 和 MaxValue 属性

c# - 将 C# 链接起来看起来像 jQuery 是一个好主意吗?

c# - 这是在 C# 中迭代​​ ConcurrentDictionary 的正确方法吗