![](/img/trans.png)
[英]EF + async = The context cannot be used while the model is being created?
[英]Building db in EF Code first, context can't be used while model is being created
我之前已經使用過幾次該代碼,並且一直可以使用,但是在這個特定項目中,我無法使用Entity Framework來構建數據庫。
當我在DbContext類中選中Autos時,我發現了這一點:
創建模型時不能使用上下文。 如果在OnModelCreating方法內部使用上下文,或者多個線程同時訪問同一上下文實例,則可能引發此異常。
這是我的OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new CompanyDBConfiguration());
modelBuilder.Configurations.Add(new ClientDBConfiguration());
modelBuilder.Configurations.Add(new QuoteDBConfiguration());
modelBuilder.Configurations.Add(new RoleDBConfiguration());
modelBuilder.Configurations.Add(new UserDBConfiguration());
modelBuilder.Configurations.Add(new InvoiceDBConfiguration());
modelBuilder.Configurations.Add(new JobDBConfiguration());
modelBuilder.Configurations.Add(new JobStatusDBConfiguration());
}
這是其中一種配置的示例:
public class CompanyDBConfiguration : DBBaseObject<Company>
{
public CompanyDBConfiguration()
: base()
{
Property(p => p.Companame)
.HasColumnName("sCompanyname")
.IsRequired();
Property(p => p.ContactPerson)
.HasColumnName("sContactPerson")
.IsRequired();
Property(p => p.ContactNumber)
.HasColumnName("sContactNumber")
.IsRequired();
Property(p => p.EmailAddress)
.HasColumnName("sEmailAddress")
.IsRequired();
Property(p => p.PhysicalAddress)
.HasColumnName("sPhysicalAddres")
.IsRequired();
Property(p => p.LicenseKey)
.HasColumnName("sLicenseKey")
.IsRequired();
Property(p => p.LicenseDate)
.HasColumnName("dtLicenseDate")
.HasColumnType("datetime")
.IsRequired();
ToTable("Companies");
}
}
誰能幫我了解這里的事嗎? 就像我說的,它已經在我完成過的每個其他項目中都起作用了,那么為什么這里不起作用呢?
提前致謝!
什么是ToTable(“公司”); 做?
我很確定這就是問題所在-除了創建數據上下文外,您不應該執行額外的工作,尤其是在該方法中調用上下文時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.