Adding a one-to-many property to the Identity Model

Lock

I have just started learning MVC 5 and the Entity Framework as well as using the Identity Model.

I am trying to add an additional property to the ApplicationUser Model that is a list of Customers. Basically, a user can have many Customers attached to their login.

I seem to have it at least displaying the list of customers on the Register.cshtml page, however I am unsure if this is the best way to do this.

In my RegisterViewModel.cs, I have added the below:

    [Display(Name = "Customer List")]
    public IEnumerable<Customer> Customers { get; set; }

Then in my AccountController, I have added to get a list of customers and pass this to the ViewModel:

    private GatewayContext gatewayContext = new GatewayContext();

    var customers = gatewayContext.Customers.ToList();
    var viewModel = new RegisterViewModel();
    viewModel.Customers = customers;
    return View(viewModel);

Is this the correct way of doing this? My worry is that I have created the gatewayContext as a property of the AccountController and not sure if this is the correct way of going about it.

Sorry if the question is a bit ambigious. I can reword if necessary! I just want to make sure I am building my application in the correct way.

Keith Payne

You're on the right track with the RegisterViewModel change.

However, it is better to use your DbContext within the controller method:

public ActionResult Register(...)
{
    List<Customer> customers;

    using (GatewayContext db = new GatewayContext())
    {
        customers = db.Customers.ToList();
    }

    var viewModel = new RegisterViewModel();
    viewModel.Customers = customers;
    return View(viewModel);
}

The idea here being to use and dispose of the context as quickly as possible. This helps by returning the Sql Server connection to the connection pool sooner, which means less waiting for a connection for other simultaneous requests.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Self referencing one to many Model

분류에서Dev

SQLAlchemy One-to-many, adding a record

분류에서Dev

Validation error during model generation in one-to-many relationship

분류에서Dev

Adding a username property to webapp2 User model after entity has been created (with Simpleauth)

분류에서Dev

Adding a property in Google Analytics

분류에서Dev

One to many on single row

분류에서Dev

Laravel Eloquent one to many

분류에서Dev

Adding OnClick property to array javascript

분류에서Dev

Django adding model id to URL

분류에서Dev

Using migrations with separate contexts for regular model and Identity

분류에서Dev

Android ListView adding too many items

분류에서Dev

Return model to controller with list property

분류에서Dev

Unable to serialize model with Highcharts property

분류에서Dev

Unable to bind selectlistitem to model property

분류에서Dev

2 forms using PHP. One for adding and one for adding changes

분류에서Dev

One-To-Many and One-To-Many relationship in Entity Framework 6

분류에서Dev

WHERE statement with one to many option

분류에서Dev

mysql query - one to many relations

분류에서Dev

One to Many entity framework error

분류에서Dev

Adding Objectify index to existing property and existing entity?

분류에서Dev

Adding ServiceStack OrmLite attributes in code instead of a property

분류에서Dev

Dynamically adding a property to an entity framework object

분류에서Dev

Accessing many-to-many details with through model in Django template

분류에서Dev

Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

분류에서Dev

Overriding model in gem, adding callback and methods

분류에서Dev

Adding params to route model breaks ArrayController

분류에서Dev

Inheriting a model and adding new field to the model odoo 12

분류에서Dev

Laravel Eloquent: In many to many relationships the ORM is returning only one Object

분류에서Dev

EditorTemplate asp-for = "Property"대 value = "@ Model.Property"

Related 관련 기사

  1. 1

    Self referencing one to many Model

  2. 2

    SQLAlchemy One-to-many, adding a record

  3. 3

    Validation error during model generation in one-to-many relationship

  4. 4

    Adding a username property to webapp2 User model after entity has been created (with Simpleauth)

  5. 5

    Adding a property in Google Analytics

  6. 6

    One to many on single row

  7. 7

    Laravel Eloquent one to many

  8. 8

    Adding OnClick property to array javascript

  9. 9

    Django adding model id to URL

  10. 10

    Using migrations with separate contexts for regular model and Identity

  11. 11

    Android ListView adding too many items

  12. 12

    Return model to controller with list property

  13. 13

    Unable to serialize model with Highcharts property

  14. 14

    Unable to bind selectlistitem to model property

  15. 15

    2 forms using PHP. One for adding and one for adding changes

  16. 16

    One-To-Many and One-To-Many relationship in Entity Framework 6

  17. 17

    WHERE statement with one to many option

  18. 18

    mysql query - one to many relations

  19. 19

    One to Many entity framework error

  20. 20

    Adding Objectify index to existing property and existing entity?

  21. 21

    Adding ServiceStack OrmLite attributes in code instead of a property

  22. 22

    Dynamically adding a property to an entity framework object

  23. 23

    Accessing many-to-many details with through model in Django template

  24. 24

    Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

  25. 25

    Overriding model in gem, adding callback and methods

  26. 26

    Adding params to route model breaks ArrayController

  27. 27

    Inheriting a model and adding new field to the model odoo 12

  28. 28

    Laravel Eloquent: In many to many relationships the ORM is returning only one Object

  29. 29

    EditorTemplate asp-for = "Property"대 value = "@ Model.Property"

뜨겁다태그

보관