C# EF Code First 使用 Fluent API 防止属性映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15130814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EF Code First prevent property mapping with Fluent API
提问by Catalin
I have a class Product
and a complex type AddressDetails
我有一个类Product
和一个复杂的类型AddressDetails
public class Product
{
public Guid Id { get; set; }
public AddressDetails AddressDetails { get; set; }
}
public class AddressDetails
{
public string City { get; set; }
public string Country { get; set; }
// other properties
}
Is it possible to prevent mapping "Country" property from AddressDetails
inside Product
class? (because i will never need it for Product
class)
是否可以防止从类AddressDetails
内部映射“国家”属性Product
?(因为我永远不需要它Product
上课)
Something like this
像这样的东西
Property(p => p.AddressDetails.Country).Ignore();
采纳答案by Twon-ha
For EF5 and older:In the DbContext.OnModelCreating
override for your context:
对于 EF5 及更早版本:在DbContext.OnModelCreating
您的上下文的覆盖中:
modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country);
For EF6:You're out of luck. See Mrchief's answer.
对于 EF6:您不走运。见Mrchief 的回答。
回答by Mrchief
Unfortunately the accepted answer doesn't work, not at least with EF6 and especially if the child class is not an entity.
不幸的是,接受的答案不起作用,至少在 EF6 中是这样,尤其是在子类不是实体的情况下。
I haven't found any way to do this via fluent API. The only way it works is via data annotations:
我还没有找到任何方法通过 fluent API 来做到这一点。它工作的唯一方法是通过数据注释:
public class AddressDetails
{
public string City { get; set; }
[NotMapped]
public string Country { get; set; }
// other properties
}
Note:If you have a situation where Country
should be excluded only when it is part of certain other entity, then you're out of luck with this approach.
注意:如果您遇到一种情况,Country
只有当它是某些其他实体的一部分时才应该被排除在外,那么您对这种方法就不走运了。
回答by Tommy Ceusters
It can be done in Fluent API as well, just add in the mapping the following code
也可以在Fluent API中完成,只需在映射中添加以下代码
this.Ignore(t => t.Country), tested in EF6
this.Ignore(t => t.Country),在 EF6 中测试
回答by JAVizcaino
If you are using an implementation of EntityTypeConfiguration you can use the Ignore Method:
如果您使用的是 EntityTypeConfiguration 的实现,则可以使用忽略方法:
public class SubscriptionMap: EntityTypeConfiguration<Subscription>
{
// Primary Key
HasKey(p => p.Id)
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.SubscriptionNumber).IsOptional().HasMaxLength(20);
...
...
Ignore(p => p.SubscriberSignature);
ToTable("Subscriptions");
}
回答by davidfcruz
On EF6 you can configure the complex type:
在 EF6 上,您可以配置复杂类型:
modelBuilder.Types<AddressDetails>()
.Configure(c => c.Ignore(p => p.Country))
That way the property Country will be always ignored.
这样,属性 Country 将始终被忽略。
回答by Jan
Try this
尝试这个
modelBuilder.ComplexType<AddressDetails>().Ignore(p => p.Country);
It worked for me in similar case.
在类似的情况下它对我有用。
回答by Salizar Marxx
While I realize that this is an old question, the answers didn't resolve my issue with EF 6.
虽然我意识到这是一个老问题,但答案并没有解决我的 EF 6 问题。
For EF 6 you need to create a ComplexTypeConfiguration Mapping.
对于 EF 6,您需要创建一个 ComplexTypeConfiguration 映射。
example:
例子:
public class Workload
{
public int Id { get; set; }
public int ContractId { get; set; }
public WorkloadStatus Status {get; set; }
public Configruation Configuration { get; set; }
}
public class Configuration
{
public int Timeout { get; set; }
public bool SaveResults { get; set; }
public int UnmappedProperty { get; set; }
}
public class WorkloadMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Workload>
{
public WorkloadMap()
{
ToTable("Workload");
HasKey(x => x.Id);
}
}
// Here This is where we mange the Configuration
public class ConfigurationMap : ComplexTypeConfiguration<Configuration>
{
ConfigurationMap()
{
Property(x => x.TimeOut).HasColumnName("TimeOut");
Ignore(x => x.UnmappedProperty);
}
}
If your Context is loading configurations manually you need to add the new ComplexMap, if your using the FromAssembly overload it'll be picked up with the rest of the configuration objects.
如果您的 Context 手动加载配置,您需要添加新的 ComplexMap,如果您使用 FromAssembly 重载,它将与其余配置对象一起被拾取。