我遇到的情况是实体框架正在将Contract_id列添加到合同模型中,并且该列不作为C#模型上的属性存在,并且它也不存在于数据库中。我已经梳理了数据库和解决方案以获得对'contract_id'的任何引用,我正在为此而烦恼。
[Export(typeof(IModel))]
[Grid.GridNavigator(cloneToTop = true, add = true, del = true, refresh = true, refreshstate = Grid.RefreshState.current, addfunc = "$.modules.grids.contract.add")]
[Grid.Events(serializeGridData = "$.modules.grids.common.serialize")]
public class Contract
: Model
, ILogicalDelete
{
#region properties
[Key]
[Grid.Column("ContractID", TypeName = "int", Label = "Contract ID", Width = 75, VisibleTo = new Type[] { typeof(Contract) })]
[Grid.Search(SearchOperators.equal)]
[Grid.Controls.Hidden]
public override int ID { get { return base.ID; } set { base.ID = value; } }
[Grid.Column("CorrelationID", Hidden = true)]
[Grid.Controls.Hidden]
public Guid? CorrelationID { get; set; }
//[Grid.Column("StatusID", ModelNavigation = "Status", HiddenFromNavigation = true)]
//[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
//public int StatusID { get; set; }
[MaxLength(100)]
[Grid.Column("ContractDesc", TypeName = "varchar", Label = "Description")]
[Grid.Search(SearchOperators.begins_with | SearchOperators.contains | SearchOperators.ends_with)]
[Grid.Controls.CustomEditable(Grid.ControlType.autocomplete, Url = "/api/contractservice/list?name=contract&column=ContractDesc&_type={0}", KeyField = "ID", TextField = "ContractDesc", ForReferenceUse = true)]
public string ContractDesc { get; set; }
[MaxLength(100)]
[Grid.Column("ContractType", TypeName = "varchar", Label = "Type", VisibleTo = new Type[] { typeof(Contract) }, HiddenFromNavigation = true)]
[Grid.Search(Grid.SearchType.select, url = "/api/contractservice/list?name=ContractType", build = "$.services.grid.ctrls.select.build")]
//[Grid.Controls.Select(Url = "/api/contractservice/list?name=ContractType", BuildSelect = "$.services.grid.ctrls.select.build")]
public string ContractType { get; set; }
[Grid.Column("Audited", TypeName = "bit", FormatterType = Grid.FormatterType.checkbox, Alignment = Grid.Position.center, Width = 50, HiddenFromNavigation = true)]
[JsonConverter(typeof(Grid.Serialization.BooleanConverter))]
public bool Audited { get; set; }
[MaxLength(255)]
[Grid.Column("Comment", TypeName = "varchar", HiddenFromNavigation = true)]
public string Comment { get; set; }
[Grid.Column("CreatedOn", TypeName = "date", Label = "Created On", FormatterType = Grid.FormatterType.date, Alignment = Grid.Position.center, Width = 75, Hidden = true, HiddenFromNavigation = true)]
public DateTime? CreatedOn { get; set; }
[Grid.Column("IsDeleted", TypeName = "bit", Hidden = true, Default = false, HiddenFromNavigation = true)]
public bool is_deleted { get; set; }
#endregion
#region constructor
public Contract()
{
Members = new HashSet<ContractMember>();
PricingTiers = new HashSet<TierMatrix>();
Terms = new HashSet<ContractDetail>();
}
#endregion
//Status _status = null;
//public virtual Status Status { get { return _status; } set { StatusID = value.IsNotNull(oObject => oObject.ID, StatusID); _status = value; } }
[JsonIgnore]
public virtual ICollection<ContractMember> Members { get; set; }
public virtual ICollection<ContractMemberDetail> MemberDetails { get; set; }
[JsonIgnore]
public virtual ICollection<TierMatrix> PricingTiers { get; set; }
[JsonIgnore]
public virtual ICollection<ContractDetail> Terms { get; set; }
}
配置:
class ContractConfiguration
: EntityTypeConfiguration<Models.Contract>
{
internal ContractConfiguration(string table, string schema = "dbo")
{
ToTable(table, schema);
HasKey(Model => Model.ID);
Property(Model => Model.ID)
.HasColumnName("ContractID")
.HasColumnType("int")
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
Property(Model => Model.ContractDesc)
.HasColumnName("ContractDesc")
.HasColumnType("varchar")
.IsOptional()
.HasMaxLength(100);
Property(Model => Model.ContractType)
.HasColumnName("ContractType")
.HasColumnType("varchar")
.IsRequired()
.HasMaxLength(100);
Property(Model => Model.Audited)
.HasColumnName("Audited")
.HasColumnType("bit")
.IsRequired();
Property(Model => Model.Comment)
.HasColumnName("Comment")
.HasColumnType("varchar")
.IsOptional()
.HasMaxLength(255);
Property(Model => Model.CreatedOn)
.HasColumnName("CreatedOn")
.HasColumnType("smalldatetime")
.IsRequired();
Property(Model => Model.CorrelationID)
.HasColumnName("CorrelationID")
.HasColumnType("uniqueidentifier")
.IsOptional();
Property(Model => Model.is_deleted)
.HasColumnName("IsDeleted")
.HasColumnType("bit")
.IsRequired();
HasMany(Model => Model.Members)
.WithRequired(Model => Model.Contract);
HasMany(Model => Model.PricingTiers)
.WithRequired(Model => Model.Contract);
HasMany(Model => Model.Terms)
.WithRequired(Model => Model.Contract);
}
}
模型构建器:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Configurations.ContractConfiguration("Contract", Common.Schema.contract_management));
}
观察这个变量: 上下文()。组(typeof运算(Models.Contract))。toString()方法
生成以下内容:
SELECT
[Extent1].[ContractID] AS [ContractID],
[Extent1].[CorrelationID] AS [CorrelationID],
[Extent1].[ContractDesc] AS [ContractDesc],
[Extent1].[ContractType] AS [ContractType],
[Extent1].[Audited] AS [Audited],
[Extent1].[Comment] AS [Comment],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[IsDeleted] AS [IsDeleted],
[Extent1].[Contract_ID] AS [Contract_ID] //<--- this is what does not belong
FROM [cm].[Contract] AS [Extent1]
我查看了数据库drop并为表创建脚本,Contract_ID没有出现在其中。我查看了与此表相关的所有外键,并确保它们是ContractID而不是Contract_ID。 Contract_ID文本在解决方案代码库中也没有出现。我们在这个数据库表上也没有任何触发器。
我用完了检查代码的地方。我正在考虑一个乐队援助,它将创建一个名为Contract_ID的计算列,该列重新调整主键值。如果有人能给我一个关于在哪里看起来会很棒的想法。
答案 0 :(得分:0)
好吧,我终于从头开始重建了C#模型,我发现额外列的原因和问题出在外键映射
[JsonIgnore]
public virtual ICollection<ContractMember> Members { get; set; }
[JsonIgnore]
public virtual ICollection<ContractMemberDetail> MemberDetails { get; set; }
[JsonIgnore]
public virtual ICollection<TierMatrix> PricingTiers { get; set; }
[JsonIgnore]
public virtual ICollection<ContractDetail> Terms { get; set; }
位于C#模型上。
从合同模型的角度来看这是映射。
HasMany(Model => Model.Members)
.WithRequired(Model => Model.Contract);
HasMany(Model => Model.MemberDetails)
.WithOptional(Model => Model.Contract);
HasMany(Model => Model.PricingTiers)
.WithRequired(Model => Model.Contract);
HasMany(Model => Model.Terms)
.WithRequired(Model => Model.Contract);
从这四个模型的角度消除了映射集合的需要。实际上包括会员的外键映射,MemberDetails,PricingTiers和Terms。对EF做了一些事情,框架逻辑决定在没有找到合同模型时需要Contract_ID列。