Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explict Value can't be inserted in Table when IDENTITY_INSERT is OFF #6006

Closed
Benny739 opened this issue Jul 6, 2016 · 6 comments
Closed

Comments

@Benny739
Copy link

Benny739 commented Jul 6, 2016

I get an error when I try to insert a value in my Table.
_dltype is an object of type BRIDownloadType. I pass it from another class.

using (var db = new BRIDatabase())
{
    foreach (var client in db.BRIClients)
    {
        var todo = new BRIToDo
        {
            BRIClient = client,
            BRIDownloadType = _dltype,
        };
        db.BRIToDos.Add(todo);
    }
    db.SaveChanges();
}

Now I get the error:

An Explict Value can't be inserted in the Idendity Column in the BRIDownloadTypes-Table when IDENTITY_INSERT is OFF.

My 2 Tables are

BRIDownloadType

public class BRIDownloadType
{
    [Key]
    public int DlTypeId { get; set; }

    [Required]
    [StringLength(15)]
    public string DlType { get; set; }

    public string Username { get; set; }

    public string Password { get; set; }

    public virtual ICollection<BRIToDo> BRIToDo { get; set; }

}

BRITodo

public class BRIToDo
{
    [Key]
    public int ToDoId { get; set; }

    [ForeignKey("BRIClient")]
    public int ClientId { get; set; }

    [ForeignKey("BRITask")]
    public int TaskId { get; set; }

    public virtual BRIClient BRIClient { get; set; }

    public virtual BRITask BRITask { get; set; }

    [ForeignKey("BRIDownloadType")]
    public int DlTypeId { get; set; }

    public virtual BRIDownloadType BRIDownloadType { get; set; }**

}

The interesting thing is, if I do something with my _dltype object, I can use it.
The following code is working but I'm still inserting the same object.

using (var db = new BRIDatabase())
{
    var dl = db.BRIDownloadTypes.FirstOrDefault(c => c.DlTypeId == _dltype.DlTypeId);
    foreach (var client in db.BRIClients)
    {
        var todo = new BRIToDo
        {
            BRIClient = client,
            BRIDownloadType = _dltype,
        };
        db.BRIToDos.Add(todo);
    }
    db.SaveChanges();
}

I just added the line and the code is working.

var dl = db.BRIDownloadTypes.FirstOrDefault(c => c.DlTypeId == _dltype.DlTypeId)

I'm using Version 1.0.0 with SqlServer in a Windows Forms Project.

@rowanmiller
Copy link
Contributor

Any chance you could provide a console app style code listing that we could run to see the issue? Without the complete code listing for your model and the context around what is stored in _dltype etc. it is hard to work out what is happening here. A project that we can run would be fine too.

@brandonseydel
Copy link

This is happening for me also

public partial class Intake
    {
        public int IntakeID { get; set; }
        public DateTimeOffset? AccidentDtm { get; set; }
        public int AddedByUserID { get; set; }
        public DateTimeOffset AddedDtm { get; set; }
        public int? ApprovedByUserID { get; set; }
        public DateTimeOffset? ApprovedDtm { get; set; }
        public int? AssignedToUserID { get; set; }
        public DateTimeOffset? CallbackAttempt1Dtm { get; set; }
        public DateTimeOffset? CallbackAttempt2Dtm { get; set; }
        public DateTimeOffset? CallbackAttempt3Dtm { get; set; }
        public int CaseTypeCategoryID { get; set; }
        public bool Deleted { get; set; }
        public int? DispositionCategoryID { get; set; }
        public DateTimeOffset DispositionDtm { get; set; }
        public string HospitalDoctor { get; set; }
        public string HowOccur { get; set; }
        public string Injury { get; set; }
        public int LastUpdatedByUserID { get; set; }
        public DateTimeOffset LastUpdatedDtm { get; set; }
        public string Notes { get; set; }
        public int PrimaryContactID { get; set; }
        public DateTimeOffset ReceivedDtm { get; set; }
        public int ReferredByCategoryID { get; set; }
        public string ReferredByOther { get; set; }
        public int TakenByCategoryID { get; set; }
        public DateTime? WorkCompReportedDt { get; set; }
        public string WorkCompReportedTo { get; set; }

        public virtual VcaseUser AddedByUser { get; set; }
        public virtual VcaseUser ApprovedByUser { get; set; }
        public virtual VcaseUser AssignedToUser { get; set; }
        public virtual Category CaseTypeCategory { get; set; }
        public virtual Category DispositionCategory { get; set; }
        public virtual VcaseUser LastUpdatedByUser { get; set; }
        public virtual Contact PrimaryContact { get; set; }
        public virtual Category ReferredByCategory { get; set; }
        public virtual Category TakenByCategory { get; set; }
    }
    public class AppDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Intake>(entity =>
            {
                entity.Property(e => e.IntakeID).ValueGeneratedOnAdd().UseSqlServerIdentityColumn();
                entity.Property(e => e.Deleted).HasDefaultValue(false);

                entity.Property(e => e.ReferredByOther).HasMaxLength(100);

                entity.Property(e => e.WorkCompReportedDt).HasColumnType("date");

                entity.Property(e => e.WorkCompReportedTo).HasMaxLength(100);

                entity.HasOne(d => d.AddedByUser).WithMany(p => p.Intake).HasForeignKey(d => d.AddedByUserID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.ApprovedByUser).WithMany(p => p.IntakeNavigation).HasForeignKey(d => d.ApprovedByUserID);

                entity.HasOne(d => d.AssignedToUser).WithMany(p => p.Intake1).HasForeignKey(d => d.AssignedToUserID);

                entity.HasOne(d => d.CaseTypeCategory).WithMany(p => p.Intake).HasForeignKey(d => d.CaseTypeCategoryID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.DispositionCategory).WithMany(p => p.IntakeNavigation).HasForeignKey(d => d.DispositionCategoryID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.LastUpdatedByUser).WithMany(p => p.Intake2).HasForeignKey(d => d.LastUpdatedByUserID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.PrimaryContact).WithMany(p => p.Intake).HasForeignKey(d => d.PrimaryContactID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.ReferredByCategory).WithMany(p => p.Intake1).HasForeignKey(d => d.ReferredByCategoryID).OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(d => d.TakenByCategory).WithMany(p => p.Intake2).HasForeignKey(d => d.TakenByCategoryID).OnDelete(DeleteBehavior.Restrict);
            });
     }
}

@ajcvickers
Copy link
Member

@Benny739 @brandonseydel You may be hitting this: #703. If your issue is not this, then we will need code showing how the context is being used in order to investigate this.

@brandonseydel
Copy link

brandonseydel commented Jul 11, 2016

Its when I add an entity and attach it it behaves this way.

this._context.Intakes.Add(intake); //Works
_context.Intakes.Attach(intake).State = EntityState.Added; //Fails

@rowanmiller
Copy link
Contributor

EF Team Triage: This issue is lacking enough information for us to be able to effectively triage it. In particular, it is missing the following information requested in the new issue template. Can you please provide this information?

Steps to reproduce

Ideally include a complete code listing that we can run to reproduce the issue.
Alternatively, you can provide a project/solution that we can run.

BTW we're not just doing this to be mean 😄... we get a lot traffic on this project and it takes time to attempt to reproduce an issue based on fragments of information. In addition, our attempt is often unsuccessful as the exact conditions required to hit the issue are often not explicitly included in the code provided. To ensure we maximize the time we have to work on fixing bug, implementing new features, etc. we ask that folks give us a self contained way to reproduce an issue.

For a guide on submitting good bug reports, read_ Painless Bug Tracking.

@rowanmiller
Copy link
Contributor

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants