MVC中向不同表插入新数据时BaseEntity的ID字段不断增加
The ID field of the BaseEntity keeps increasing when insert a new data to different tables in MVC
我正在使用工作单元和存储库制作一个 MVC 项目。我将模型、控制器和映射等分离到解决方案中的不同库中。当我使用基础实体时,我碰巧遇到了一个问题,即在这种情况下,我用于基于基础实体的多个 table 的 ID 增加了。当我向 Table A 插入新数据时,EX 的 ID 自动增加:1,但是当我向 Table B 插入新数据时,ID 增加其值 A 和第一个数据Table B 的 ID 值为 2。我如何设法将 table 之间的 ID 分开以增加 TABLE 的当前值而不是 BASE ENTITY 的当前值TABLE 基于 ?
这是我的基础实体:
namespace Thaco_Model.Model
{
public interface IBaseEntities
{
long ID { get; set; }
String name { get; set; }
}
public class BaseEntities : IBaseEntities
{
[Key]
public long ID { get; set; }
public String name { get; set; }
public HttpStatusCode StatusCode { get; set; }
public Boolean IsDelete { get; set; }
public int creator_ID { get; set; }
public Nullable<int> editor_ID { get; set; }
public Nullable<DateTime> created_Date { get; set; }
public Nullable<DateTime> edited_Date { get; set; }
}
}
这是我的 table A
namespace Thaco_Model.Model
{
[Table("ValueFlag")]
public class Value_Flag :BaseEntities
{
}
}
这是我的 table B :
namespace Thaco_Model.Model
{
[Table("DefinedValue")]
public class Defined_Value : BaseEntities
{
public long flag_ID { get; set; }
}
}
这是我的存储库:
namespace Data
{
public class Repository<T> : IRepository<T> where T : BaseEntities
{
private readonly DB_ver_5 context;
private IDbSet<T> entities;
string errorMessage = string.Empty;
public Repository(DB_ver_5 context)
{
this.context = context;
}
public T GetById(object id)
{
return this.Entities.Find(id);
}
public void Insert(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.Entities.Add(entity);
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
}
}
throw new Exception(errorMessage, dbEx);
}
}
public void Update(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
throw new Exception(errorMessage, dbEx);
}
}
public void Delete(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.Entities.Remove(entity);
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
throw new Exception(errorMessage, dbEx);
}
}
public virtual IQueryable<T> Table
{
get
{
return this.Entities;
}
}
private IDbSet<T> Entities
{
get
{
if (entities == null)
{
entities = context.Set<T>();
}
return entities;
}
}
}
}
实际上,这是正常的,在 EF 之前。
您有一个具有主键的基本实体 class。当您将此 class 设为其他人的父级时,Entity Framework 正在考虑您确实想要共享属性。如果查看数据库,您应该会看到 ValueFlag
没有 ID
。 DefinedValue
也是如此。这是因为您的共享属性必须在名为 BaseEntity
的 table 中。通过这样做,EF 可以实现 "sharing properties".
如果您想要单独的 ID,您需要将 EF 行为从 TPT 更改为 TPC:https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines
此外,请考虑继承通常很好,对于小型 tables 来说已经足够好了。当涉及到大数据时,由于 JOINS 的乘法,它显示出一些性能问题(除了 EF "normal" 性能问题)。
我正在使用工作单元和存储库制作一个 MVC 项目。我将模型、控制器和映射等分离到解决方案中的不同库中。当我使用基础实体时,我碰巧遇到了一个问题,即在这种情况下,我用于基于基础实体的多个 table 的 ID 增加了。当我向 Table A 插入新数据时,EX 的 ID 自动增加:1,但是当我向 Table B 插入新数据时,ID 增加其值 A 和第一个数据Table B 的 ID 值为 2。我如何设法将 table 之间的 ID 分开以增加 TABLE 的当前值而不是 BASE ENTITY 的当前值TABLE 基于 ? 这是我的基础实体:
namespace Thaco_Model.Model
{
public interface IBaseEntities
{
long ID { get; set; }
String name { get; set; }
}
public class BaseEntities : IBaseEntities
{
[Key]
public long ID { get; set; }
public String name { get; set; }
public HttpStatusCode StatusCode { get; set; }
public Boolean IsDelete { get; set; }
public int creator_ID { get; set; }
public Nullable<int> editor_ID { get; set; }
public Nullable<DateTime> created_Date { get; set; }
public Nullable<DateTime> edited_Date { get; set; }
}
}
这是我的 table A
namespace Thaco_Model.Model
{
[Table("ValueFlag")]
public class Value_Flag :BaseEntities
{
}
}
这是我的 table B :
namespace Thaco_Model.Model
{
[Table("DefinedValue")]
public class Defined_Value : BaseEntities
{
public long flag_ID { get; set; }
}
}
这是我的存储库:
namespace Data
{
public class Repository<T> : IRepository<T> where T : BaseEntities
{
private readonly DB_ver_5 context;
private IDbSet<T> entities;
string errorMessage = string.Empty;
public Repository(DB_ver_5 context)
{
this.context = context;
}
public T GetById(object id)
{
return this.Entities.Find(id);
}
public void Insert(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.Entities.Add(entity);
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
}
}
throw new Exception(errorMessage, dbEx);
}
}
public void Update(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
throw new Exception(errorMessage, dbEx);
}
}
public void Delete(T entity)
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this.Entities.Remove(entity);
this.context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorMessage += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
throw new Exception(errorMessage, dbEx);
}
}
public virtual IQueryable<T> Table
{
get
{
return this.Entities;
}
}
private IDbSet<T> Entities
{
get
{
if (entities == null)
{
entities = context.Set<T>();
}
return entities;
}
}
}
}
实际上,这是正常的,在 EF 之前。
您有一个具有主键的基本实体 class。当您将此 class 设为其他人的父级时,Entity Framework 正在考虑您确实想要共享属性。如果查看数据库,您应该会看到 ValueFlag
没有 ID
。 DefinedValue
也是如此。这是因为您的共享属性必须在名为 BaseEntity
的 table 中。通过这样做,EF 可以实现 "sharing properties".
如果您想要单独的 ID,您需要将 EF 行为从 TPT 更改为 TPC:https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines
此外,请考虑继承通常很好,对于小型 tables 来说已经足够好了。当涉及到大数据时,由于 JOINS 的乘法,它显示出一些性能问题(除了 EF "normal" 性能问题)。