Validation errors during model generation
I'm working what I guess is a code-first EF 4.1 implementation. The database exists already and the basic models exist already (generated by XSD.exe for my incoming XML data). I've written my own mappings and updated the models to behave properly with Entity Framework.
When I try to run the code I'm receiving the below error:
One or more validation errors were detected duri开发者_Python百科ng model generation:
System.Data.Edm.EdmEntityType: Name: Each type name in a schema must be unique. Type name 'CourtCase' is already defined.
I've checked and doubled checked and definitely only have this class defined in one location. I've also used to below line to keep the DbContext from trying to create its own models.
Database.SetInitializer<LoadContext>(null);
My XML data is being deserialized into my models and then I'm simply trying to save it to the database:
var serializer = new XmlSerializer(typeof (CourtCase));
var xmlReader = new XmlTextReader(
new MemoryStream(Encoding.Default.GetBytes(_itemText)));
_deserializedCase = (CourtCase) serializer.Deserialize(xmlReader);
Database.SetInitializer<LoadContext>(null);
using (var dbContext = new LoadContext())
{
dbContext.Cases.Add(_deserializedCase);
dbContext.SaveChanges();
}
DbContext:
public class LoadContext : DbContext
{
public DbSet<CourtCase> Cases { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new CaseMap());
etc.
}
}
Am I missing another key component of Entity Framework configuration?
Full Stack Trace
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
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.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)
Essentially I found a set of classes that created a circular reference A.B.C.A
(the original classes were generated from a very poorly designed XML Schema). Once I removed the circular reference this error was resolved.
Whether it is generated on the fly or available at design time, you probably have an EDMX file within your solution, right? It would be here that I would be checking. Remember that there are three layers within this schema - the storage, conceptual and mapping layers. I would check each of these layers (not sure that this applies to the mapping layer) for duplicate entities - the CourtCase entitiy in particular.
If your EDMX is generated on the fly then I would write out a copy to the file system so that you can more easily inspect it.
I would be interested to know if execution makes it past the point at which you are deserializing the XML data. This would help to rule out the XML data as the source of the problem.
Edit: I think that your original post specified "database first". I am not as familiar with "code first" but did find this great walkthrough while investigating.
Edit2: After a little more investigation My guess is that the 'other' type is the result of a convention. Experiment with removing some of these conventions.
精彩评论