我想仅使用数据注释来映射多对多关系,所以这就是我的情况。
假设我有下表,
Foo:
FooId int identity
FooName varchar(max) not null
Bar:
BarId int identity
BarName varchar(max) not null
FooBarRelationships
TheBarId int
TheFooId int
如您所见,连接表包含与源不同的 FK 名称。另外表的名称也不同
我的类如下
[Table("Foo")]
public class Foo {
[Key]
public Int32 FooId { get; set; }
public String FooName { get; set; }
public IEnumerable<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar {
[Key]
public Int32 BarId { get; set; }
public String BarName { get; set; }
public IEnumerable<Foo> Foos { get; set; }
}
我应该如何使用数据注释修改我的类以便告诉表FooBarRelationships
是一个联结表和TheBarId 是来自 Bar
表的外键,TheFooId
是来自 Foo
表的外键?
聚苯乙烯
将导航属性声明为 IEnumerable
,而不是 ICollection
至关重要。
我尝试使用地图类来映射它,如下所示,
public class BarMap : EntityTypeConfiguration<Bar> {
public BarMap() {
this.HasMany(t => t.Foos)
.WithMany(t => t.Bars)
.Map(m => {
m.ToTable("FooBarRelationships");
m.MapLeftKey("TheBarId");
m.MapRightKey("TheFooId");
});
}
}
这给了我以下错误。
方法“System.Data.Entity.ModelConfiguration.EntityTypeConfiguration.HasMany”的类型参数(System.Linq.Expressions.Expression>>)' 无法从用法中推断出来。尝试显式指定类型参数。
如果有一种方法可以在不将类型 IEnumeralbe
更改为 ICollection
的情况下解决此错误,也欢迎。
I want to map a many to may relationship using ONLY data annotations so this is my case.
Say I have the following table
Foo:
FooId int identity
FooName varchar(max) not null
Bar:
BarId int identity
BarName varchar(max) not null
FooBarRelationships
TheBarId int
TheFooId int
As you can see the junction table contains FK names different from source. In addition the name of the table is also different
My classes are as follows
[Table("Foo")]
public class Foo {
[Key]
public Int32 FooId { get; set; }
public String FooName { get; set; }
public IEnumerable<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar {
[Key]
public Int32 BarId { get; set; }
public String BarName { get; set; }
public IEnumerable<Foo> Foos { get; set; }
}
How should I modify my classes using data annotations in order to tell that the table FooBarRelationships
is a junction table and TheBarId
is the foreign key from Bar
table and TheFooId
is the foreign key from Foo
table?
P.S.
Having navigation properties declared as IEnumerable<T>
, not as ICollection<T>
is essential.
I've tried to map it using the map class which looks as follows
public class BarMap : EntityTypeConfiguration<Bar> {
public BarMap() {
this.HasMany(t => t.Foos)
.WithMany(t => t.Bars)
.Map(m => {
m.ToTable("FooBarRelationships");
m.MapLeftKey("TheBarId");
m.MapRightKey("TheFooId");
});
}
}
this gives me the following error.
The type arguments for method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Bar>.HasMany<TTargetEntity> (System.Linq.Expressions.Expression<System.Func<Bar, ICollection<TTargetEntity>>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
If there is a way to resolve this error without changing the type IEnumeralbe<T>
to ICollection<T>
, it is also welcomed.
发布评论
评论(2)
我认为您不能使用 IEnumerable 因为实体框架需要可以分配的对象。
EF 4.1 中是否支持 IEnumerable 集合进行映射?
你应该从另一个角度来解决这个问题。
最后一部分:
您可以在集合两侧使用 InverseAttribute 来声明多对多关系。
I don't think you ca use IEnumerable because entity framework needs object that can be assigned.
Are IEnumerable collection supported in EF 4.1 for mapping?
You should be attacking the problem from another angle.
last part:
you can use the InverseAttribute on both side of your collection to declare the many to many relationship.
如果您不使用流畅映射,则必须使用默认约定所需的联结表名称及其列,该名称应类似于:
FooBars
,其中包含列Foo_FooId
和Bar_BarId
。另外,正如 @Inu
IEnumerable
的回答中提到的,不支持 - 至少 延迟加载代理需求ICollection
,我几乎确定这是全局要求(如链接答案中所述)。 Code First 通常会跳过映射中的所有IEnumerable
属性。即使它以某种方式支持IEnumerable
只是一个接口,后面会有一些类,如Collection
、HashSet
或List< /code> 因此公开可枚举不会限制使用实体将导航属性转换为该集合类的代码。
If you don't use fluent mapping you must use the name of junction table and its columns expected by default convention which should be something like:
FooBars
with columnsFoo_FooId
andBar_BarId
.Also as mentioned in answer by @Inu
IEnumerable
is not supported - at least lazy loading proxies demandICollection
and I'm almost sure that it is global requirement (as mentioned in linked answer). Code first normally skips allIEnumerable
properties from mapping. Even it it is somehow supportedIEnumerable
is just an interface and on behind there will be some class likeCollection
,HashSet
orList
so exposing enumerable will not restrict code using your entity to convert your navigation property to that collection class.