Creating Model for Database View - asp.net

To create my model i followed the below steps
I have already have the database so i added an ADO model to my Project under Model folder.
ADO model created the classes for me and i can use them.
I have created a view(not View of MVC) in my DB. But when i am trying to add my ADO model it is failed since it has no Primary key.
Now i am trying to access my DbView. What is the best solution for this ? I am not able to use LinqtoSql since my DB is Oracle.
The other option that i can write a model class to access my DbView ? But how can i write ?
Regards

I have solved this problem. If one of my column has "not null" property, EF define this coloum n as a PK. So i define a new column in my view and generate GUID for this column.
I think it will help someone .
Regards.

Related

How to access the table AspNetUserRoles generated by ASP identity?

I am coding an API that uses the Identity feature on asp.net and right now I am trying to make a method that returns all the couples ( user.id, role.id ) which are stored on the table AspNetUserRoles when assigning a role to a user.
One solution would be to get the full list of users and find each role of that user in a double loop but having too much data, it wouldn't be optimal for my case. The perfect solution for me would be to access that table directly which contains exactly what I am looking for
Thanks for the help!
You will have to create class models and relationships explicitly as described in the documentation below:
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-3.1#add-user-and-role-navigation-properties
This documentation expects you to at least create the ApplicationUser, ApplicationRole and ApplicationUserRole class models. Also, you will have to update your DBContext accordingly to correctly use the new models that you create. The names for the models can be changed. However, in any case, this should not change any DB schema and will not require any EF migrations.
Lastly,

Entity Framework update issue

I am not sure if it's a bug, but when i add a new view or a new stored procedure to the model it updates all the tables that exist. So my question is should it work like this and if it should how can i add some new procedure without updating the whole model?
Yes, this is the correct functionality when using the "Update Model" function for EntityFramework. It looks at every database object and updates the EF Model to match what it finds in the database. This is, in part, because the designer does not let you specifically choose which tables or view to update, so it verifies any changes in the database. This allows the model to proactively ensure that when it connects to the database there won't be an error based on the database changing.

Adding table from another database to ASP.NET Dynamic Data + Entity Framework

I have a table in another database I would like to scaffold via ASP.NET Dynamic Data and incorporate into my existing Entity Model - is there anyway to do this? (eg using a view or other mechanism or customize the view, edit or insert operations via ad-hoc SQL or stored procedures?)
I don't want to replicate the entire DynamicData sub-folder structure and create another entity model for just one table
I was able to solve this by manually creating an entity in the SSDL and CSDL sections of the .edmx file by using a DefiningQuery and then defining the EntitySets for my entity class
I also added insert / update / delete Function elements to the SSDL with inline SQL using the CommandText property
At this point I had enough to let the Designer map the CRUD methods to these inline SQL functions I defined
It's a little tricky but it works and the general approach opens up many possibilities I had not thought about

MVC3 Entity Framework using default membership relationships

I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
This has few preconditions:
ASP.NET tables must be in the same database as your own tables
Previous precondition means that you must either create your database and tables manually (without automatic code-first generation) or you must use some custom initializer which will add non mapped ASP.NET tables as part of database recreation
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example MembershipUser doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.

How do I define a database view using Entity Framework 4 Code-First?

How do I define a database view using Entity Framework 4 Code-First? I can't find anything about this anywhere!
That's because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can't define such constructs using code first.
If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer - it will be similar like this answer. Just be aware that this will not help you if you want to map entity to a view. In such case you would probably have to first drop table created by EF and create view with the same name (I didn't try it but it could do the trick). Also be aware that not every view is udpatable so you will most probably get read only entity.
To do a view you create the model, then in the initializer you run the SQL statement to create the view directly against the context with the first line of code, and then in the context you override OnModelCreating and run the second line of code to ignore the model.
context.Database.ExecuteSqlCommand(Resources.<resourcename>);
modelBuilder.Ignore<modeltype>();

Resources