首页 > 解决方案 > 不支持使用“身份”模式修改列。列:'ID'。表:'CodeFirstDatabaseSchema.BidTo'

问题描述

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

配置:

    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;”行,我都会收到错误消息 注释掉与否。

添加:一个注意,这个表,但不是其他两个具有相同错误的表有一个 INSERT/UPDATE/DELETE 触发器。

标签: sql-serverasp.net-mvcentity-framework

解决方案


推荐阅读