I am new to MVC, I received a project from my client, the project is build on MVC, I am unable to identify what approach as been used to in the application. Whether it's code first approach or a database first approach. How to identify it. The developer who designed the project is no more associated with the company so I can't seek his help.
It will be database first if there is an [name].edmx diagram file and some templates (.tt). You can also search for onModelCreating and see if it has the statement throw new UnintentionalCodeFirstException(); which would also indicate it is database first with an edmx.
Also, if it is EF Core it will be Code First.
Related
At work, I use ASP.NET MVC (not .NET Core) with Entity Framework and SQL Server. However, I wanted to practice a bit with ASP.NET Core MVC and some other SQL Server stuff (like experimenting with SSIS) at home, so I set up a small dev environment for me to work in. I created my database and tables, populated it with information, and created the default template you get with ASP.NET Core MVC. No issues so far!
But then, when I went to add a model into my project, I had no option to add an ADO.NET Entity Data Model like I do at work. After googling around a bit, I saw a mention that you could not create these kinds of models in .NET Core applications: https://learn.microsoft.com/en-us/answers/questions/357012/can39t-find-adonet-entity-data-model-missing-visua.html. I did some more googling, and it seems like just about every single thing I'm finding online is that you have to use package manager console, and type it all out.
Is this really the only way? Surely there has to be something better... It was very nice in ASP.NET MVC when I could add the model, and then it would take me through a wizard to get everything set up. I could create a new connection string, test the connection to the database, select which tables/views I wanted to add to the model, and I was all set! It was just as nice being able to go into my model and easily update the model with new tables, columns, or anything else I needed.
I get that code-first solutions are more mainstream these days, but I want to stick with the database first approach. Is there a simple, user-friendly (non package-manager console) approach for me to add models like I used to, but still get the benefits of .NET Core? Any good tutorials out there to get me on the right track (that aren't code first)? I appreciate anyone who can point me in the right direction!
I'm looking for some guidance here... I have an old ASP.NET webforms site with an existing database and SQL Server membership, and I'm creating a new interface using ASP.NET MVC and Entity Framework with a database-first approach.
So I have tables as shown below:
My problem is, I'm unsure how to make use of these old membership tables for authentication in the new ASP.NET MVC application.
I don't mind whether I migrate the user/roles data to ASP.NET Identity or continue using the old membership database but I'm not sure what the best approach is. I think I would prefer to migrate the data, as I am more familiar with Identity.
What I've tried...
While searching for solutions I have come across this: Migrating an Existing Website from SQL Membership to ASP.NET Identity several times, however this (and most of what I've found so far) is talking about upgrading an existing webforms site to use asp.net identity. I have tried using parts of it to migrate the Membership data to asp.net identity, but of course the remaining instructions don't fit my scenario.
So here's where I'm at now.
I have used the SQL script in the walkthrough linked above to migrate membership data to asp.net identity tables.
I have set up SQLPasswordHasher as described in the walkthrough, since I figure I'll need that function to use existing logins.
I imported those tables to the database that contains the website data, in hopes that I'd only have to deal with a single connection string.
I modified ApplicationDbContext : base... to use that database (it was previously using DefaultConnection (LocalDb)
So now I have these tables populated with data:
Aaand I'm stuck.
It's entirely possible that I'm missing something basic, but anyway if I try to login or register a new user in it's current state I get the error:
The entity type ApplicationUser is not part of the model for the current context.
on the line:
var result = await UserManager.CreateAsync(user, model.Password);`
The only solutions I've been able to find for that error have been on code-first projects: something to do with migrations, which I'm not using (I did try it but received an error on enable-migration "Creating a DbModelBuilder or writing the EDMX from a DbContext created using database-first or model-first is not supported.")
UPDATE
Ok so I came across an answer regarding the The entity type ApplicationUser is not part of the model for the current context. error, apparently using the connection string generated by entity framework can be problematic, so as advised, I made a 'normal' connection string and this seems to be getting closer!
UPDATE 2
I think I have it working, will run some tests though before updating with my solution.
Please follow the below step to do this.
Create a new ASP.NET MVC Project with user authentication.
Now right click on Model folder and add a new item "ADO.NET Entity Data model" enter your model name and select ok now select Database first approach and provide your database name and connection and connection name to be saved in web.config file.
Now go to your web.config file and you will see two connection are defined in your connection string and each connection point to different database , one for IDENTITY and other for which you have added just now.
for eg:
you need to change the data source of the both connection to be same.
So whenever you need to access user info you need to use ApplicationDbContext and for others you can use you 'CustomConn' context which you have defined earlier while adding new model.
Hope this will help you !
I am sorry if I am asking something that has an obvious answer, but I have spent and entire day searching for resources on the subject and I fail to find or understand how to do a few basic thing with EF7.
So, here is my question.
I have an ASP MVC 5 (VNEXT) website and I am using Entity Framework 7. I have an existing database, thus I am working database-first.
So far everything was fine. I installed everything required to get my DNX EF commands up and working; I scaffolded a dbContext and I got all my tables as classes and a dbContext class.
Everything fine, all well. I was happy and continuing with my work.
However, I got to a point where I wanted to make a property of one of the generated (table) classes Required, because I use jQuery unobtrusive validation.
I have the following resource as a reference: http://ef.readthedocs.org/en/latest/modeling/required-optional.html
My first wonder is, according to this source, in the FluentAPI the property has been marked as .IsRequired(). I believe, making it required here is a whole other thing that has nothing to do with unobtrusive validation. So, the next thing explained is simply - go to your class and add the Required data annotation.
This is all fine and well, and after adding it, it works as it should.
But I immediately wondered - well, I am modifying the generated classes, am I not going to lose those changes once I update the model?
Which leads me to my final problem - I searched for a long time, I even played with the help menu of DNX EF, but I am unable to find the right way to update the dbContext and generated models after I make changes to the database.
I believed this to be something quite trivial but to my surprise I am unable to find a resource explaining how to do the update.
Can you point me in the right direction, and tell me how to update EF generated models and context after I make changes in the database schema, and what is the best way to add annotations to the properties of the generated classes?
The general consensus is that you shouldn't use your database entities as input from users directly. Instead, use ViewModels, verify those against your validation rules, then map them to database transactions.
As asp.net MVC developer I use database first and updating database is a big head ache so I use Metadata approach and create ViewModels which helps allot.
I'm just now teaching myself MVC4 (did webform for years), and I'm
frustrated - but not about MVC which is pretty good. Entity Framework
is...well
I'm using VS2010.
The problem
We have a real database, you know normalized with like foreign keys and stuff. But every example I find for Entity Framework is direct to the table, but we rarely have a straight table pull - out side of populating dropdowns and such. All of our frontend calls hit a Stored Proc (how old school! expletive deleted)
I love the Model architecture of MVC where you define the attributes of the data coming from the data source - Display Name, Ranges, DataType. etc. So I definitely want to keep this.
Entity Framework and MVC do not want to play well in this senario. I created my edmx file (with SPs only), did my function import for a SP, everything is good....'til now.
Can't Create a Controller from the edmx/designer - Enter Controller Name, pick MVC controller with read using EF, pick the model class that is the FuntionName_Result, and for context pick the ...Entities name. FAIL Unable to retrieve metadata
OK, so now I try EF 5.x DbContext Generator, update the file name and boom I have a model and context - awesome now I can do the cool MVC stuff, lets rebuild the site....oh the horror - everything has been previously defined.
I tried generating the edmx in a different folder and/or deleting it after the DBContext generator, still can't create Controller.
'blah' is not part of the specified 'Context' class, and the 'Context' class could not be modifed to add a 'DbSet' property to it. (For example, the 'Context' class might be in a compiled assembly.)
IF I manually add the DBSet, I'm back to unable to retreive metadata - I am assuming this is happening because it can't connect to the DB. I don't know where to tell it to use the connection string in web.config. - if this is the problem
Here lies dead my MVC hopes of a brighter future.
What am I missing?
I am not married to EF, so if there is a better way to access databases (without writing all the code from scratch) I'm here to listen.
Thanks
Entity framework relies heavily on conventions. It takes a little bit to get used to. For example for connection strings... If entity framework doesn't find a connection string with the same name as your DBcontext class it just makes one (i think it defaults to using the project name as the database name). If this database doesn't exist it will create it locally as a sql express DB. This leads to the kinds of errors like the ones you are reporting.
If you want to define the connection string for entity framework all you need to do is provide a connection string in the web.config. Again conventions.... the connection string should be named the same as your DBContext class and entity framework will just find it.
<connectionStrings>
<add name="MyDbContextClassName" connectionString="..." />
</connectionStrings>
On an architecture note, IMHO ORM's are defiantly the way to go for new application development. It makes getting data into and out of your database soo much easier. That said it is a Big paradigm shift if you are used to accessing everything via sprocs and direct queries to the DB. Don't give up on it. It will frustrate you at first just like picking up any new tech, but it's well worth it in the end.
I have used entity framework and nHibernate for ORM in the past. The thing that I like about entity framework is that if you use code first migration most of the really annoying, tedious, and error prone column mappings are auto generated for you (again using conventions). You sometimes still end up having to do a little mapping but those cases are pretty rare. Maybe a little less rare if you already have a database since your column names will probably not always match entity frameworks conventions. Anyway... This is a big plus in my book and why I would defiantly favor EF over nHibernate.
If you already have a database, with stored procedures you do not need the EF 5.X DbContext Generator. Create either a folder in your project or a new project in your solution for your data access. In that folder/project add a edmx file and configure it to an existing database using the wizard. At this stage you can pull in your stored procedures.
If you open the edmx file, you can go to the model explorer tab and manage the imported functions (stored procs) and their return types.
Once you have that, In your controller, rather than using an instance of DbContext you can just use an instance of your EF Entities. So if you called your edmx 'MyDbAccess' you should be able to use MyDbAccessEntities which will then allow you to access the stored procedures.
I in your App.config file check your
<connectionStrings>
After check if your stored procedures are added in the model context file
YouDBModel.Context.tt
> YourBDModel.Context.cs
I want to develop an ASP.Net MVC application with EF4 Model First design and only generate the actual database much later, ideally at the end of the project.
Based on some of the ideas here:
http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/
I want to create something like an InMemoryObjectContext for testing and development and use IOC to switch to the SQL Server implamentation (EF generated) for UAT and production.
Is this wise?
Is it possible? If so, does anyone have any suggestions?
Does EF always need an underlying database in order to track changes, commit etc?
I've tried creating a model first but as soon as I add properties I get the following errors:
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer Model1Container.
and the warning:
Running transformation: Please overwrite the replacement token '$edmxInputFile$' with the actual name of the .edmx file you would like to generate from.
The error doesn't stop the application running but worries me. I'm very very new to EF so I apologize if this is way off the mark or a dumb question. I'm hoping to get some good advice while I sit for the next few days and watch videos and read articles.
Thanks
Davy
At the very least you need mapping information "filled in". You can fill these fields with nonsense if you don't want to work against the underlying database.
If your doing Model first, right click on the designer canvas and select, "Generate Database from Model". This will automatically create convention based mappings for you without defining tables and columns. You don't even need a valid db connection to do this.