我有一个旧的数据库优先EF 6.1模型,我想将其转换为代码优先。
我使用了Microsoft的EF Power Tools来执行逆向工程。但是由于我已经进行了少量调整,我重用了由DB-first的T4模板生成的旧.cs
文件。这些调整包括(据我记得):
sottogruppi
,它映射到sottogruppi
类和sottogruppi
DbSet。我使用大写S
现在我已经找到了一个好的DbContext,看起来如下(仅为几个实体提供示例)
public class ContosoModel {
public virtual DbSet<SezioneA> sezione_a { get; set; }
public virtual DbSet<SezioneD> sezione_d { get; set; }
public virtual DbSet<SezioneE> sezione_e { get; set; }
public virtual DbSet<SezioneF> sezione_f { get; set; }
public virtual DbSet<SezioneH> sezione_h { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
modelBuilder.Configurations.Add(new SezioneAMap());
modelBuilder.Configurations.Add(new SezioneDMap());
modelBuilder.Configurations.Add(new SezioneEMap());
modelBuilder.Configurations.Add(new SezioneFMap());
modelBuilder.Configurations.Add(new SezioneGMap());
modelBuilder.Configurations.Add(new SezioneHMap());
}
}
public partial class SezioneA : IStandardColumns
{
public bool IsValid(bool isStornoContabile)
{
if (isStornoContabile)
{
return A54a != "4";
}
else
{
return A54a != "2" && A54a != "3";
}
}
public string A01 { get; set; }
public string A01a { get; set; }
public string A01b { get; set; }
public string A02 { get; set; }
public string A03 { get; set; }
public string A11 { get; set; }
public string A12 { get; set; }
public string A12a { get; set; }
public string A12b { get; set; }
public string A12c { get; set; }
public string A21 { get; set; }
public string A22 { get; set; }
public string A23 { get; set; }
public string A24 { get; set; }
public string A25 { get; set; }
public string A31 { get; set; }
public partial class SezioneA : ISharedColumns
{
public string A01 { get; set; }
public string A01a { get; set; }
public string A01b { get; set; }
public string A02 { get; set; }
public string A03 { get; set; }
public string A11 { get; set; }
public string A12 { get; set; }
public string A12a { get; set; }
public string A12b { get; set; }
public string A12c { get; set; }
public string A21 { get; set; }
public string A22 { get; set; }
public string A23 { get; set; }
public string A24 { get; set; }
public string A25 { get; set; }
public string A31 { get; set; }
[...plenties more...]
}
public SezioneAMap()
{
// Primary Key
this.HasKey(t => new { t.progA, t.utente });
// Properties
this.Property(t => t.A01)
.IsRequired()
.HasMaxLength(13);
this.Property(t => t.A01a)
.IsRequired()
.HasMaxLength(2);
this.Property(t => t.A01b)
.IsRequired()
.HasMaxLength(11);
}
等等......
当我尝试使用Web.xml支持的Mysql设置启动应用程序时,我收到一个关于真正不属于的实体类型的错误
这是web.xml
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<!--codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">-->
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<add name="connString"
connectionString="Server=HOST;Database=DB;Uid=root;Pwd=root;Allow Zero Datetime=True;CharSet=latin1;Pooling=True;Min Pool Size=45"
providerName="MySql.Data.MySqlClient" />
Com.Contoso.Model.Expression: : EntityType 'Expression' has no key defined. Define the key for this EntityType.
Com.Contoso.Model.ElementInit: : EntityType 'ElementInit' has no key defined. Define the key for this EntityType.
Com.Contoso.Model.MemberBinding: : EntityType 'MemberBinding' has no key defined. Define the key for this EntityType.
Com.Contoso.Model.MemberInfo: : EntityType 'MemberInfo' has no key defined. Define the key for this EntityType.
Com.Contoso.Model.SwitchCase: : EntityType 'SwitchCase' has no key defined. Define the key for this EntityType.
Com.Contoso.Model.CatchBlock: : EntityType 'CatchBlock' has no key defined. Define the key for this EntityType.
Expressions: EntityType: EntitySet 'Expressions' is based on type 'Expression' that has no keys defined.
ElementInits: EntityType: EntitySet 'ElementInits' is based on type 'ElementInit' that has no keys defined.
MemberBindings: EntityType: EntitySet 'MemberBindings' is based on type 'MemberBinding' that has no keys defined.
MemberInfoes: EntityType: EntitySet 'MemberInfoes' is based on type 'MemberInfo' that has no keys defined.
SwitchCases: EntityType: EntitySet 'SwitchCases' is based on type 'SwitchCase' that has no keys defined.
CatchBlocks: EntityType: EntitySet 'CatchBlocks' is based on type 'CatchBlock' that has no keys defined.
堆栈追踪:
System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() +320
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +588
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +138
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +196
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +276
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +791
System.Data.Entity.Internal.InternalContext.Initialize() +52
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +53
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +193
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +58
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +141
System.Linq.Queryable.Cast(IQueryable source) +47
Com.Contoso.NewsManager.FindAllValid() in NewsManagerImpl.cs:16
问题是直截了当的:我该如何解决这些问题?我是否需要粘贴其他信息?
其他信息:
HasKey
调用更新:
这可能是由不规则的关系映射引起的。我只能认为HasRequired
/ HasOptional
/ WithMany
等等System.Linq.Expressions.Expression
。我正在调查更多,如果找到不合适的地方,我会发布额外的代码
答案 0 :(得分:0)
我不明白为什么会这样,但我的问题已经解决了。
modelBuilder.Ignore<System.Linq.Expressions.Expression>();
我正确地猜到这与LINQ相关的类有关,可能是因为内置的约定?我不知道。但绝对违规的实体名称Contoso.Model.Expression
完全不正确。