gpt4 book ai didi

c# - Entity Framework 验证错误没有定义键。为此 EntityType 定义键

转载 作者:行者123 更新时间:2023-11-30 12:47:18 24 4
gpt4 key购买 nike

当我尝试在我的数据库上下文中保存一个对象时,我遇到了这个 Entity Framework 错误。我不知道从哪里开始寻找。

这里会发生什么?

如何找出问题所在?

谢谢。

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

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Exception' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'AssemblyName' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'IdentityReference' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Exceptions' is based on type 'Exception' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'AssemblyNames' is based on type 'AssemblyName' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'IdentityReferences' is based on type 'IdentityReference' that has no keys defined.

at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel model)
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 entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at MVCDaemon.Controllers.AwardBadgesController.FromUnprocessedEvents() in Hobby Stuff\Hehe\Hehe.Daemon\Controllers\AwardBadgesController.cs:line 58}

和我的其他东西:

DaemonDB.Tasks.Add(T);

public class DaemonDBContext : DbContext
{
public DaemonDBContext() : base("DefaultConnection") { }

//Collections also in BaseDBContext:
public DbSet<Badge> Badges { get; set; }
public DbSet<BadgeGroup> BadgeGroups { get; set; }
public DbSet<Event> Events { get; set; }

//Collections only in DaemonDBContext
public DbSet<EventCheckedForBadgeReward> EventCheckedForBadgeReward { get; set; }
public DbSet<Task> Tasks { get; set; }
}

public class Task
{
public int ID { get; set; }
public string FriendlyTitle { get; set; }
public DateTime TimeOfExecution { get; set; }
public TimeSpan LengthOfExecution { get; set; }
public Stopwatch StopWatch;
public Exception ExceptionEncountered { get; set; }

public Task() {
TimeOfExecution = DateTime.Now;
}

public void StartStopwatch()
{
StopWatch = Stopwatch.StartNew();
StopWatch.Start();
}

public void StopStopwatch()
{
StopWatch.Stop();
}

public string GetStatusAsFriendly()
{
return ExceptionEncountered == null ? "Success" : "Failed";
}
}

最佳答案

必须在某些属性上使用 [NotMapped] 属性。

关于c# - Entity Framework 验证错误没有定义键。为此 EntityType 定义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17355666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com