我有一个对象,其中包含一个具有另一个对象类型的属性,我想将其视为复杂类型。
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在添加迁移时,我遇到了需要主键的问题(正是我想要防止的)。
实体类型Coordinate需要定义一个主键。
编辑
出于性能原因,我希望将属性存储为Coordinate_Latitude而Coordinate_Longitute不是引用另一个表。
运行这样的代码时出现以下错误:
var dbContext = new MyDbContext();
var list = dbContext.Set<TEntity>().ToList();
Run Code Online (Sandbox Code Playgroud)
从我最近做的更改到代码我理解,因为我向基类添加了一个事件,它会导致所有问题:
public PropertyChangedEventHandler PropertyChangedEvent { get; set; }
Run Code Online (Sandbox Code Playgroud)
将NotMapped属性应用于上述属性,我的代码现在再次运行.
现在我想知道是否有自动告诉EntityFramework不映射特定类型的属性(这不是我自己的类型,我不能将任何属性应用于.Net的类型).
例外:
Sequence does not contains any element.
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(DbDatabaseMapping databaseMapping, EdmEntityType entityType)
at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(EdmAssociationType associationType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.Generate(EdmAssociationType associationType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAssociationTypes(EdmModel model, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel model)
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel model, DbProviderManifest providerManifest)
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.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type …Run Code Online (Sandbox Code Playgroud)