I'm using ASP.NET Core 6 and Entity Framework.
I have 4 classes (meat, dairy, breads and notdiaryOrMeat) that all inherit from class Food.
In the SQL class all the dbsets for the inherited classes but only class Food appears in SQL Server.
Why can't I see the different classes in SQL Server?
Related
I have generated my Database layer (Database First Entity Model) by using entity framework 6.1. Now all the classes generated are in the same project as well as in the same namespace i.e. Example.DataAccessLayer. For separation of concerns, I have moved my entity classes(POCO classes) that are under Model.tt file to separate project and under namespace Example.DataModel. The reason I have done this is because then I can use Example.DataModel project in my websites so that the DbContext classes are not visible and all the data management is done through my business layer i.e. Example.BusinessLayer.
Now doing this I have to give reference of my Example.DataModel project to Example.DataAccessLayer. As it is suggested in some of the tutorial, after doing this one has to change the Custom Tool Namespace of MyModel.Context.tt file to Example.DataModel so the entity classes are visible. But by doing this the DBContext and DataModel comes under same namespace that is Example.DataModel.
Now the question is, is there a way to generate my entity model with my context and entities classes in separate projects and in separate namespace without giving my DBcontext.tt file custom tool namespace ?
Why not use Code-First to connect to you existing DB? Then you have full control of where Models and DbContext lives.
I have a project about survey application on web. I have to use MVC 4 Code First and I have to use abstract class to derive other classes. I just need a logic of this structure and coding. I've shared my database schema with you below which is built on MySQL Workbench. I want to use Code First to make this schema possible for my Model Classes. I need to finish this project until Monday.
Schema :
http://imageshack.com/a/img547/4278/q2or.png
I'm new to Entity Framework (I'm using EF5), and I'm working on a test project to learn something about it.
Now I'm implementing some DAO classes to access DB following this tutorial (it's a bit old...)
I generated entity classes using Entity Data Model tool, but now I need all entities to inherit from EntityObject class.
Reading this I expected it to be so, but actually my classes inherit only from IObjectWithChangeTracker, INotifyPropertyChanged.
So I'm wondering if I'm doing something wrong...
Do I have to set some configuration in generation tool?
You need to use the correct template from http://msdn.microsoft.com/en-us/data/jj613116.aspx - one of those that are called "EntityObject Generator".
I have been using Linq-to-SQL for a while to get access to my database. But I have recently been told this way of doing was not the best one since it allows to mix the data access & business logic layers.
I heard that Entity Framework T4 POCO was a solution but I cannot find complete information about it. Does anyone have more details to share with me ?
Thanks in advance
What a POCO (Plain Old CLR Object) does it that it allows you to create your own representation class of your database. Entity Framework then converts your database (through a configuration (hint use an edmx file)) to the by you created POCO classes.
Example:
Table User:
id | fName | lName | otherField
You can represent this in your C# with a POCO to a user object with the following properties:
int id, string fName, string lName, var otherField.
Then you can, in the getters and setters of these properties, insert your business logic.
NOTE: I'd recommend using just the Entity Framework icm with an edmx file. And put your business logic somewhere else. When creating a web service I always like the following order of classes :
A class that receives the calls and calls the right functions of the next class
This class then converts the given params in the call into a format that the rest of the application understands and calls the right functions of another class.
This class then checks the business logic in the params and calls another class to do something with the database.
This class then handles the database connection and stuff (with use of the Entity Framework) Note again: you can also use POCO's in this last step ;)
See that: Entity Framework - Generating Classes
There is a tutorial how to generate POCO Classes by existing database.
I find this site gives a great example on how to use EF4 with POCO classes.
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
This describes the 'code first' approach for Entity Framework 4
My Visual Studio solution has the following architecture :
a "DataAccess" project, containing one EDMX with his "object context" class (but without any generated business class)
an "Entities" project, containing the business entities generated from the EDMX. (ex : Customer class, Order class, Product class, etc).
a "Web" project containing the ASP.Net GUI.
Now I want to add a small Silverlight area in my existing application, with RIA Services.
I don't succeed in creating the domain Service : the "Add new domain service class" wizard contains my ObjectContext but itself contains no entities. It seems that it's because the EDMX is NOT in the same assembly as my entities.
Now I don't want to rebuild my existing application with a new architecture by merging the 2 projects (actual architecture seems clean for me).
How can I make this %$$$! wizard see my entities ? Should I construct the domain service class manually ? How ?
Thanks !
EDIT : I'm using C# (4), EF 4, Silverlight4
EDIT 2 : my entities are generated with the "ADO.NET POCO Entity Generator".
Should I construct the domain service class manually ?
I reckon so. It's the same using EF 5 with POCOs, the wizard doesn't pick up the entities. There are some helpful snippets on Colin Blair's site for creating the CRUD methods over the DbContext.