I am new to EF CodeFirst and trying to build relations between entities.
I've three tables and their relationship are as follows:
When I pass UserID I should get all the provider details of Organizations for which this user belongs to.
How to build the entities and their relations for this scenario in CodeFirst(fluent API) approach? I am using EF CodeFirst on existing database. So these three tables already exists in Database.
Any inputs or pointers will be appreciated. Thank you!
Use EF Power Tools. It has Reverse Engineer option that will create a model for an exisiting database. If you want to configure your model with fluent API instead of attributes you can use the above to get you to a starting point where you would manually convert whatever needs to be converted.
Related
The Users in my database will be related to many other entities. Is the recommended practice for doing this to have 1 db context for the app? Or, should there be two different ones. It seems that I could retrieve the context through the use of the GetOwinContext()
The standard practice with this is to keep all of your tables for your webapp in 1 database (1 context). One large advantage of this is that you can execute SQL joins based on data that is stored in your Identity tables.
Here is a use case: I have a fileupload entity and table which I need to relate to a specific user (the user that uploaded the file). If everything lives in one database then I can use foreign keys and entity framework navigation properties as I normally would. If I was using a separate database/context for the Identity provided tables, then I would need to query two different database (which is more costly performance-wise) to get my needed data. One query to DB1 to get my user Id and another query to DB2 to get the fileuploads which belong to this user Id
So in short, if you have users related to many other entities (which you mentioned) then I would strongly recommend using 1 database and context.
I've made an application in ASP.NET MVC. I first created an database in SQL Server and then installed the Entity Framework in Visual Studio and used the Entity Data Model Wizard, I selected the 'Code First from database', and it generated the derived DbContext Class and the models from the database.
My question is basically, why is it called 'Code First' when all the classes are generated based on the database - I've basically written no code, apart from the SQL.
Normally Code first refers to generating the database from your POCO but typically when you are targeting an existing database you can have the VS tools create the classes for you to get up and running quickly.
That is the impression I go from typing Code First from database into google.
Code-First from an Existing Database:
Entity Framework provides an easy way to use code-first approach for
an existing database. It will create entity classes for all the tables
& views in your existing database and configure it with
DataAnnotations attributes and Fluent API.
Additional source:
ScottGu's Blog - Using EF “Code First” with an Existing Database
EF “Code First” works great with existing databases, and enables a
very nice code-centric development approach with them. In particular,
it enables you to use clean “plain old classes” (aka POCO) for your
model objects, and cleanly map them to/from the database using either
the default mapping conventions or by overriding them with custom
schema mapping rules.
My team would like to separate User data from our core application data. We're using SQL Server 2014 and EF 6.x Code First. If we create a Users database and an Application database is there any way to tell EF about a relationship between the User object and and Order object? Assuming the Orders are stored in the application database and the users are stored in the Users database. We know that we could instantiate the User from the Users database and then query the Application database for a list of orders, but what we're looking for is a solution that functions as if the tables were both in the same database, such as when a User is retrieved its list of Orders is available without a second call.
Basically, we're looking for a way to map entities to a specific context and then instantiate each context pointing to its respective database and let EF sort out the calls.
Yes, but you need to do some configuration one the database servers.
Your Application database can define a Synonym to the table in the Users database, which you can then map an entity to as if it were a normal table in the Application DB.
As far as Entity Framework is concerned, it uses the same DbContext pointing to your Application database, but it can pull information from your Users database.
One downside with this is that your Application database needs to be setup to point at a specific Users database.
I am using Entity Framework Model First. There are 2 entities in my Model..
User
Role
I want to use MVC scaffolding (using VS 2012 or 2010) for this relationship.
From some posts in 20111 many-many was not supported. is it still not supported? If nto supported is there any workaround?
I can not change the Model (because database will be generated from it) however i am mainly looking forward creating Users, Creating Roles and Assigning roles to Users. if some workaround using one-many can give me above scaffolded views and controller I will be ok with that.
Thanks
Nachi
I am reading an article about Entity Framework 4.0 that states the following:
"The model's context menu has an option to 'Generate Database Script from
Model'. When you select this option you'll find that you do need to point
to an existing database. The script won't create the database itself,
just the schema, which means that you'll need to create the database yourself
in advance."
If the EF 4.0 designer generates SQL to clobber the existing database, why is an existing database first required ?
Thanks,
Scott
It will generate the tables/indexes etc. for you. You just need to supply an existing , empty, database.
You'd run in to a chicken and egg problem otherwise, as you'd have to e.g. provide a connection string pointing to an existing database to be able to connect to a database in the first place.
And while there might have been some options to have EF generate the database as well, it's likely not worth the implementation trouble. Just poiint EF at an existing but empty database.