我正在从 Visual Studio 2017 迁移到 Visual Studio 2022,并收到有关“值不能为空。参数名称:类型”的 EF 错误,因此我将实体框架 6.2.0 更新为 EF 6.4.4。现在我收到此错误,表示
ConceptualModels
标签不匹配(当尝试更新我的数据库时)。我如何检查我的 ConceptualModels
并解决此问题?
(CodeFirst 项目)
错误:
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
at System.Data.Entity.Migrations.Edm.ModelCompressor.Decompress(Byte[] bytes)
at System.Data.Entity.Migrations.DbMigration.GetModel(Func`2 modelAccessor)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Infrastructure.Design.Executor.Update.<>c__DisplayClass0_0.<.ctor>b__0()
at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
The 'ConceptualModels' start tag on line 4 position 6 does not match the end tag of 'ConceptualModessociationSet'. Line 1747, position 7.
我的数据库上下文:
namespace GMM.Repository.Context
{
public class GMMDbContext : IdentityDbContext<ApplicationUser, AppRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public static string ConnectionString { get; set; }
static GMMDbContext()
{
#if DEBUG
ConnectionString = "GMMConnection";
#endif
}
public static GMMDbContext Create()
{
return new GMMDbContext();
}
public GMMDbContext()
: base(ConnectionString)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<GMMDbContext, GMM.Repository.Migrations.Configuration>());
DbInterception.Add(new FtsInterceptor());
}
#region AccessControl
public IDbSet<GroupRole> GroupRoles { get; set; }
public IDbSet<RoleGroup> RoleGroups { get; set; }
public IDbSet<UserRoleGroup> UserRoleGroups { get; set; }
#endregion
#region BaseInformation
public IDbSet<Department> Departments { get; set; }
#endregion
#region General
public IDbSet<Location> Locations { get; set; }
public IDbSet<Setting> Settings { get; set; }
#endregion
public int SaveChanges(string currentUserIp)
{
AddTimestamps(currentUserIp);
return base.SaveChanges();
}
public Task<int> SaveChangesAsync(string userIp)
{
AddTimestamps(userIp);
return base.SaveChangesAsync();
}
public override int SaveChanges()
{
AddTimestamps();
return base.SaveChanges();
}
private void AddTimestamps(string currentUserId, string currentUserIp)
{
if (currentUserId == null)
{
throw new NullReferenceException("CurrentUserId");
}
var entities = ChangeTracker.Entries().Where(x => x.Entity is IBaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));
foreach (var entity in entities)
{
if (entity.State == EntityState.Added)
{
((BaseGuidEntity)entity.Entity).DateCreated = DateTime.Now;
((BaseGuidEntity)entity.Entity).CreatedBy_Id = currentUserId;
((BaseGuidEntity)entity.Entity).CreatedByIp = currentUserIp;
}
((BaseGuidEntity)entity.Entity).DateModified = DateTime.Now;
((BaseGuidEntity)entity.Entity).ModifiedBy_Id = currentUserId;
((BaseGuidEntity)entity.Entity).ModifiedByIp = currentUserIp;
}
}
private void AddTimestamps()
{
var entities = ChangeTracker.Entries().Where(x => x.Entity is IBaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));
foreach (var entity in entities)
{
if (entity.State == EntityState.Added)
{
((BaseGuidEntity)entity.Entity).DateCreated = DateTime.UtcNow;
}
((BaseGuidEntity)entity.Entity).DateModified = DateTime.UtcNow;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
#region Message
modelBuilder.Configurations.Add(new PrivacyConfig());
modelBuilder.Configurations.Add(new ResultConfig());
modelBuilder.Configurations.Add(new SubjectConfig());
modelBuilder.Configurations.Add(new PriorityConfig());
modelBuilder.Configurations.Add(new CategoryConfig());
modelBuilder.Configurations.Add(new MessageConfig());
modelBuilder.Configurations.Add(new MessageFeedbackConfig());
modelBuilder.Configurations.Add(new ReferConfig());
#endregion
base.OnModelCreating(modelBuilder);
}
}
}
我的模型图:
我刚刚从项目中删除了 DBContex 并添加了新的 Context 然后使用 EF v6.4.4 和 Visual Studio 2022 复制粘贴我的整个旧模型结构。 我的问题解决了。 Visual Studio 2017 XML 解析器或创建器似乎与 Visual Studio 2017 不同。 所以当我用 vs2022 创建旧的 XML 结构模型时,问题就解决了