none
why not getting foreign key property into scaffold template in mvc controller and view? database generated using EF codeFirst RRS feed

  • Question

  • Product.cs

      public partial class Product : GeneralDBId
        {
            public string productName{ get; set; }
        }

    Order.cs

    public partial class Order : GeneralDBId
            {
                public string orderName{ get; set; }
                 public virtual Product product { get; set; }
            }

    GeneralDBId.cs

    [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid MyID{ get; set; }

    Database generated using entity framework codefirst. tables created like below

    dbo.Product
    MyID (PK)
    productName
    
    
    
     dbo.Order
       MyID (Pk)
       orderName
       Product_MyID (FK)

    If I use ProductID and OrderID as PK then I can able to use MVC scaffold template perfectly.

    But When I try to use my Custom PK like myID

    I'm facing issue to use MVC scaffold template as for FK it is not generating dropdown and mapping on View but it is creating perfect PK FK mapping in tables how so?


    Sunday, November 8, 2015 12:07 PM

All replies

  • The first thing I see is you are drinking the MS Kool Aid, and you are using EF right up there in the MVC controller that may lead to you getting burnt.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    <copied>

    Encapsulation is a means of information hiding. Layered designs in information systems are another embodiment of separation of concerns (e.g., presentation layer, business logic layer, data access layer, persistence layer).

    <end>

    http://www.codeproject.com/Articles/70061/Architecture-Guide-ASP-NET-MVC-Framework-N-tier-En

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    Sunday, November 8, 2015 7:36 PM
  • If I use `CustomerID` and `ProductID` as PK then I can able to use MVC scaffold template perfectly.

    But When I try to use my Custom PK like `myID`

    I'm facing issue to use  MVC scaffold template  as for FK it is not generating dropdown and mapping on View but it is creating perfect PK FK mapping in tables how so?

     

    Ashish Fugat (ashuthinksatgmail.com) SE

    Monday, November 9, 2015 5:21 AM
  • If I use `CustomerID` and `ProductID` as PK then I can able to use MVC scaffold template perfectly.

    You don't need EF to do MVC scaffolding. An EF entity is just a custom object, which you can make yourself to hold data. That's why there is a Model folder (objects) that can be implemented in the MVC solution, and you leave the EF entities at the DAL they never come past the DAL, if you understand separation of concerns.  

    http://www.w3schools.com/aspnet/mvc_folders.asp

    I'm facing issue to use  MVC scaffold template  as for FK it is not generating dropdown and mapping on View but it is creating perfect PK FK mapping in tables how so?

    Well, like I said. You are drinking the MS Kool Aid on the MVC controller and EF just a little too hard.

    Monday, November 9, 2015 2:42 PM
  • ok , Yes I perfectly understand your point of code separation i just want to know the way to use Pk Fk mapping that I did I want to create crud operations with mvc using my new tables where pk is myID instead ProductId n other

    Ashish Fugat (ashuthinksatgmail.com) SE

    Monday, November 9, 2015 4:38 PM
  • ok , Yes I perfectly understand your point of code separation i just want to know the way to use Pk Fk mapping that I did I want to create crud operations with mvc using my new tables where pk is myID instead ProductId n other

    Ashish Fugat (ashuthinksatgmail.com) SE


    Well, someone else is going to have to help you on that one. That's why I use the DB first always to avoid the complications you are facing.  :)
    Monday, November 9, 2015 5:21 PM