使用 EF 在数据库中添加新表后,系统无法正常工作
本文关键字:常工作 工作 系统 数据库 EF 添加 新表 使用 | 更新日期: 2023-09-27 18:28:49
嗨,我正在我的 asp.net mvc 5 应用程序中使用 webAPI。 最初,当我只有货币和用户输入时,我的应用程序运行良好,但是当我在数据库中添加新表时(即 Companies
和 CompanyCurrencies
( primary & FK relations
它抛出异常,如下所示:
类型的异常 "System.Data.Entity.Core.EntityCommandExecutionException" 发生在 EntityFramework.SqlServer.dll但未在用户代码中处理
其他信息:执行命令定义时出错。
当我将数据从Data Access Layer
返回到middle layer
时,错误出现
private DataAccessLayer db = new DataAccessLayer();
public List<Currency> GetAll()
{
return db.Currencies.ToList<Currency>();
}
将业务对象链接到数据库表Data access layer
如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Currency>().ToTable("Currency");
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Company>().ToTable("Companies");
modelBuilder.Entity<CompanyCurrency>().ToTable("CompanyCurrencies");
}
public DbSet<Currency> Currencies { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<CompanyCurrency> CompanyCurrencies { get; set; }
模型:
public class Company
{
[Key]
[Required]
public long CompanyCode { get; set; }
[Required]
public string CompanyName { get; set; }
...
}
public class CompanyCurrency
{
[Key]
public long RecordNo { get; set; }
public long CompanyCode { get; set; } //I have made it FK in db
[Required]
[StringLength(3)]
[RegularExpression("^[A-Z]{3,3}$")]
public string CurrencyCode { get; set; } //and this one also
public bool IsDefault { get; set; }
}
public class Currency
{
[Key]
[Required]
[StringLength(3)]
[RegularExpression("^[A-Z]{3,3}$")]
public string CurrencyCode { get; set; }
[Required]
[StringLength(20)]
public string CurrencyName { get; set; }
public string MajorUnit { get; set; }
public string MinorUnit { get; set; }
}
关于这个问题,我认为我已经创建了新表 与主要和外关键关系,会有某种方式 在 EF 中定义这些关系,但我不知道确切的 问题是?这只是我的猜测。
所以任何人都可以在这方面帮助我,这对我有很大的帮助。提前致谢
以下是错误的完整详细信息;
System.InvalidOperationException 未由用户代码处理 HResult=-2146233079
消息=导航属性
CurrencyCode
不是声明的 类型CompanyCurrency
上的属性。验证它是否尚未 从模型中显式排除,并且它是有效的导航 财产。源=实体框架
堆栈跟踪:
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociations(EntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(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.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at eGLVersion1._0.Data.CurrencyProvider.GetAll() in d:'Laptop Backup'Proglib'eGLVersion1.0'eGLVersion1.0.Data'Providers'CurrencyProvider.cs:line 26
at eGLVersion1._0.Web.Controllers.CurrencyWebAPIController.Get() in d:'Laptop Backup'Proglib'eGLVersion1.0'eGLVersion1.0.Web'Controllers'CurrencyWebAPIController.cs:line 31
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException:
我认为您是对的,例外与某些未定义的关系有关。 您可以通过类中的属性属性或使用 OnModel 中的 Fluent API 定义关系创建类似
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Entity<CompanyCurrency>()
.HasForeignKey(companyCurrency => companyCurrency .companyId);
}
请注意,我只输入了伪代码来指向正确的方向。 有关详细信息,请查看关系和导航属性和 EF Fluent API
编辑:对不起@UzairAshraf我的错,如果你使用Ef 6或更高版本,请使用HasRequired和HasOptional。由于您使用的是 Attribute,因此通过定义虚拟属性在模型类中执行此操作更为简单。
'public class Company
{
[Key]
[Required]
public long CompanyCode { get; set; }
[Required]
public string CompanyName { get; set; }
...
}'
public class CompanyCurrency
{
[Key]
public long RecordNo { get; set; }
public long CompanyCode { get; set; } //I have made it FK in db
public virtual Company Company { get; set; }
[Required]
[StringLength(3)]
[RegularExpression("^[A-Z]{3,3}$")]
public string CurrencyCode { get; set; } //and this one also
public bool IsDefault { get; set; }
}
这将生成表作为
CreateTable(
"dbo.CompanyCurrencies",
c => new
{
RecordNo = c.Long(nullable: false, identity: true),
CompanyCode = c.Long(nullable: false),
CurrencyCode = c.String(nullable: false, maxLength: 3),
IsDefault = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.RecordNo)
.ForeignKey("dbo.Companies", t => t.CompanyCode, cascadeDelete: true)
.Index(t => t.CompanyCode);