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

Time:09-12

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 >
                <label for="dropdownrole">دسته بندی :</label>
                <div >
                    <select  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 >acepte</div>
                <div >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; }
 }

CodePudding user response:

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; } 
}

CodePudding user response:

BookcategFK if it is a foreign key which refers the value of primary key in another table, so check if the value of BookcategFK is exists in the other table.

EDIT your implantation isn't clear to me, try it this way

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }


    public List<Category> GetCategories()
    {
        return new List<Category>()
    {
        new Category() { Id = 1, Name = "History" },
        new Category() { Id = 2, Name = "Art" }
    };
    }
}


public class Book
{
    public int Id { get; set; }
    public bool DS { get; set; }
    public string BookName { get; set; }
    public int CategoryId { get; set; }
    public string Publisher { get; set; }
    public Category Category { get; set; }  



    public List<Book> GetBooks()
    {
        return new List<Book>()
        {
            new Book() {
                Id = 1,
                BookName = "ABC",
                DS = true,
                CategoryId = 1,
                Publisher = "The Pulisher",
            },

            new Book() {
                Id = 2,
                BookName = "XYZ",
                DS = true,
                CategoryId = 1,
                Publisher = "The Pulisher",
            },

        };
    }
}
  • Related