使用' Identity'修改列模式不受支持。专栏:' ID'。表:' CodeFirstDatabaseSchema.BidTo'

时间:2018-05-22 14:39:23

标签: sql-server asp.net-mvc entity-framework

直到今天早上这个问题都不存在。但是,现在,当我尝试插入一个不是主键的标识字段的表时,我得到了上述错误。

配置:

    public BidToConfiguration()
    {
        ToTable("BidTo");

        HasKey(bt => new { bt.BidRecipientCode, bt.ProposalNumber });

        Property(bt => bt.Awarded)
            .IsOptional();
        Property(bt => bt.BidRecipientCode)
            .IsRequired()
            .HasMaxLength(10);
        Property(bt => bt.BidValue)
            .IsRequired();
        Property(bt => bt.ContactEmail)
            .IsOptional()
            .HasMaxLength(150);
        Property(bt => bt.ContactName)
            .IsOptional()
            .HasMaxLength(150);
        Property(bt => bt.ContactPhone)
            .IsOptional()
            .HasMaxLength(25);
        Property(bt => bt.ID)
            .IsRequired()
            .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
        Property(bt => bt.LastChanged)
            .IsRequired();
        Property(bt => bt.LastChangedBy)
            .IsRequired()
            .HasMaxLength(50);
        Property(bt => bt.ProposalNumber)
            .IsRequired()
            .HasMaxLength(15);
        Property(bt => bt.ReqForBidNumber)
            .IsOptional()
            .HasMaxLength(50);

        HasRequired(bt => bt.BidRecipient)
            .WithMany(br => br.BidTos)
            .HasForeignKey(bt => bt.BidRecipientCode);
    }

插入此表:

                    BidTo newBidTo = new BidTo();

                    newBidTo.Awarded = bt.Awarded;
                    newBidTo.BidRecipientCode = bt.BidRecipientCode;
                    newBidTo.BidValue = bt.BidValue;
                    newBidTo.ContactEmail = bt.ContactEmail;
                    newBidTo.ContactName = bt.ContactName;
                    newBidTo.ContactPhone = bt.ContactPhone;
                    newBidTo.ID = bt.ID;
                    newBidTo.LastChanged = DateTime.Now;
                    newBidTo.LastChangedBy = User.Identity.Name;
                    newBidTo.ProposalNumber = proposal.ProposalNumber;
                    newBidTo.ReqForBidNumber = bt.ReqForBidNumber;

                    _dbpm.BidTos.Add(newBidTo);

当我执行context.SaveChanges()时,无论是否有行" newBidTo.ID = bt.ID;"我都会收到错误。是否评论。

ADDED:一个注意事项,这个表,但没有其他两个具有相同错误的表有一个INSERT / UPDATE / DELETE触发器。

0 个答案:

没有答案