Search code examples
c#entity-framework-coreasp.net-core-mvccrud

How work with foreign key in Entity Framework Core & ASP.NET Core MVC


Update Qustion: Part of my problem solve and thank to every one my create problem solve. for the show, now i write this code but i do not know how to show this to the view:

public IEnumerable<object> Paging(int page)
    {
        int t = (page - 1) * 10;
        DB db = new DB();

        //var q = db.Books.Skip(t).Take(10).ToList();

        var q = (from i in db.Books
                 select new
                 {
                     i.id,
                     i.BookName,
                     i.Writer,
                     i.BookcategFK.BookCategori,
                     i.inventory,
                     i.BannerImage,
                     
                 }
                 ).Skip(t).Take(10).ToList();

        return q;
    }

I create two tables book and type, and define a foreign key between them, but when I try to write data in the foreign key column, I get an error. This problem exists when I try to read them too.

Please help.

This is my controller code:

UploadFile UP = new UploadFile(Environment);

Book B = new Book();

BLBook BLB = new BLBook();

B.BookName = MBM.BookName;
B.BookcategFK.id = MBM.BookcategFK; // i get error in this line
B.CoverType = MBM.CoverType;
B.PublishedNumber = MBM.PublishedNumber;
B.BannerImage = UP.uploadBook(MBM.BannerImage);
        
B.Atleast = MBM.Atleast;
B.inventory = MBM.inventory;
B.Price = MBM.Price;
B.Discount = MBM.Discount;

BLB.Create(B,MBM.BookcategFK); 
        
return View("BookCreate");

This is my view markup:

  <div class="form-group">
                <label for="dropdownrole">دسته بندی :</label>
                <div class="custom-control custom-radio xmt-3">
                    <select class="custom-select mt-3" id="BookcategFK" name="BookcategFK">
                        @{
                            if (!Model.Any())
                            {

                            }
                            else
                            {
                                foreach (var item in Model)
                                {
                                    <option value="@item.id">@item.BookCategori</option> // i get error here too
                                }
                            }
                        }


                    </select>
                </div>
                <div class="valid-feedback">acepte</div>
                <div class="invalid-feedback">fill it please</div>
            </div>

This is my model code:

namespace Online_Book_Shop.Models
{
public class BookModel
{
    public int id { get; set; }

    public bool DS { get; set; }

    public string BookName { get; set; }

    public int BookcategFK { get; set; }

    
    public string Publisher { get; set; }
}

This is my business entity class:

public class Book
{
    public int id { get; set; }

    public bool DS { get; set; }
    public string BookName { get; set; }

    public BookCategorization BookcategFK { get; set; }
    public string Publisher { get; set; }
 }

Solution

  • For foreign key, need to mark with [ForeignKey] attribute.

    using System.ComponentModel.DataAnnotations.Schema;
    public class Book 
    { 
        public int id { get; set; } 
        public bool DS { get; set; } 
        public string BookName { get; set; } 
        [ForeignKey("BookcategFK")] // this name should match the field name in other class
        public BookModel BookcategFK { get; set; } 
        public string Publisher { get; set; } 
    }