实体框架生成具有数据库或代码第一个模型中不存在的列的SQL



i有一个情况,即实体框架将合同列添加到合同模型中,并且该列不作为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));
    }

观看此变量:context((。set(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]

我已经审查了数据库的删除,并为表格创建脚本,而Contract_ID没有出现。我已经审查了与此表相关的所有外国钥匙,并确保它们不是Contractid而不是Contract_ID。文本Contract_ID在解决方案代码库中也没有。我们在此数据库表上也没有任何触发器。

我用尽了该代码的位置。我正在考虑一个乐队援助,该乐队将创建一个称为Contract_ID的计算列,该列会反省主要的钥匙值。如果有人能给我一个想法,那真是太棒了。

好吧,我终于重建了从头开始的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);

从这四个模型的角度从映射收集中消除了需求。实际上,包括有关会员,成员尾尾,定价和条款的外国钥匙映射。框架逻辑决定在没有找到合同模型上需要合同列的框架逻辑上的某些事情。

相关内容

  • 没有找到相关文章

最新更新