2011-02-15 74 views
4

考虑以下模型:EF代码优先CTP5 - 使用属性作为列名的名称外键

public class Foo 
{ 
    public int Id { get; set;} 
    public Bar TheBar { get; set; } 
} 

public class Bar 
{ 
    public int Id { get; set;} 
} 

EF尝试生成的FK列BarId

如何使其使用TheBar

我已经试过如下:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Foo>() 
       .HasOptional(x => x.Bar) 
       .WithMany() 
       .IsIndependent() 
       .Map(x => x.MapKey(bar => bar.Id, "TheBar")); 
} 

但试图使用上下文时,我得到以下异常:

Exception has been thrown by the target of an invocation. 
---> System.InvalidOperationException: Sequence contains more than one matching element 

Server stack trace: 
    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) 
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.IndependentAssociationMappingConfiguration`1.Configure(DbAssociationSetMapping associationSetMapping) 
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action) 
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) 
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) 
    at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo, Boolean validateModel) 
    at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection providerConnection) 
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel() 
    at System.Lazy`1.CreateValue() 

Exception rethrown at [0]: 
    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) 
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.IndependentAssociationMappingConfiguration`1.Configure(DbAssociationSetMapping associationSetMapping) 
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action) 
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) 
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) 
    at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo, Boolean validateModel) 
    at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection providerConnection) 
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel() 
    at System.Lazy`1.CreateValue() 
    at System.Lazy`1.LazyInitValue() 
    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_Provider() 
    at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 

回答

4

错误是由CTP5中的错误引起的。如果Id属性是在基类中定义的(由于我认为它没有关联,所以在上面的代码中没有显示),它会中断。

我通过定义一个基本接口来解决它,但在每个类中保留了Id。

0

要在fluent interfaceMapKey方法。或者,如果您在整个模型中都这样做,则CTP 5具有可插入的约定,尽管这是AFAIK,但尚未完整记录。

+0

我已经尝试过使用MapKey(与FNH或者ConfORM相比,它非常令人困惑),但是在使用上下文时,我从EF的肠子中得到了“序列包含多个匹配元素”异常。 – 2011-02-15 21:15:18

+0

查看增加的信息。 – 2011-02-15 21:19:12