How can I insert a row in my database that has a foreign key without adding a row in the linked table?

c# entity-framework-6 foreign-keys sql sql-insert

Question

I created an Entity Model with Database First on my project. In my database i have two table´s. One for books and one for the author´s. The books have a foreign key on the authortable. And every time I want to add a new book, im adding a new author, even if the specified author already exists or I got an error.

This is are my classes:

Book

    public partial class BOOK
    {
        public int ID { get; set; }
        public string TITLE { get; set; }
        public int PAGENUMBER { get; set; }
        public int AUTHOR { get; set; }

        public virtual AUTHOR AUTHOR1 { get; set; }
    }

Author

    public partial class AUTHOR
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public AUTHOR()
        {
            this.BOOK = new HashSet<BOOK>();
        }

        public int ID { get; set; }
        public string FIRSTNAME{ get; set; }
        public string LASTNAME{ get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<BOOK> BOOK { get; set; }
    }

I have tried in several ways to just add the book and when I created it I specified the author or added the new book to the author's list. Most of the time it results in an error and the first time the author was added to the author table a second time. Maybe I'm missing something obvious or don't know a certain method because I'm an absolute beginner with the Entity Framework.

EDIT: I have always tried to get the author via a select query before and pass it to the book as an object.

var functions= new DbFunctions();
var author = functions.SelectAuthor(8); //Select with the ID

var book = new BOOK()
    {
        AUTHOR1 = author,
        TITLE= "ABC",
        PAGENUMBER = 30,
    };

    var context = new EF6_DbFirst_DatabaseEntities();
    context.BOOK.Add(book);
    context.SaveChanges();

That returns this error: System.InvalidOperationException: 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'

1
0
4/7/2020 5:38:46 AM

Popular Answer

I found the simple answer. If I just give the author ID to AUTHOR while creating the book it works. I have missed this simple way again and again.

var functions= new DbFunctions();
var author = functions.SelectAuthor(8); //Select with the ID

var book = new BOOK()
    {
        AUTHOR = author.ID, //this simple Change will did it correct
        TITLE= "ABC",
        PAGENUMBER = 30,
    };

    var context = new EF6_DbFirst_DatabaseEntities();
    context.BOOK.Add(book);
    context.SaveChanges();
0
4/7/2020 5:44:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow