Is there any way to create one-to-many and many-to-many relationships without having to use virtual fields? I am asking because I would be happy to reuse models created for the code first migration, but I do not know what to do with those virtual fields. I just need a plain model without features like that.
P.S. I am using EF only for migration, so there is no issue with breaking the framework.
The virtual keyword is not necessary. They are only necessary if you want to achieve Lazy-Loading for navigation properties. If you are using EF only for migration, you could remove the virtual keyword without problems.
Related
I have a database model that can be modify by users at runtime:
adding new columns to existing tables
adding new tables
I want to use Entity Framework Core to access such model.
I'm able of creating the types for the new tables and fields using reflection but I'm not able of creating the DbSet members inside the DbContext class for these new types as the DbSet needs to know the type at compile time.
Does anyone know if this is something that can be achieved with EF Core?
A way of injecting the type to the DbSet member dynamically?
It sounds pretty weird to me that the users are the ones defining the tables and their columns, relationships, etc on runtime. Probably what you actually need is to have a structure of tables to support dynamic data, which is much more manageable, that is, a table that defines the UserModels, another table that defines the properties of those models, etc. That will vary a lot depending on your needs.
You could also consider using some special properties like XML data-type fields as suggested here: Dynamically adding a property to an entity framework object
We've been working for along time using EF Designer from database, so switching to another approach is not an option for us.
Due to a legacy version of our app i need to create relationship between tables on EF level for the current version, so those relationships don't exist in the schema.
I understand that i can do do that using Code First from database approach, but the problem how do i map the fields and make these entities part of the main datacontext which we already extended its functionalities using another partial class.. the main problem now is just mapping the new entities fields
i can't create OnCreatingModel in a new datacontext class since it does exist in the auto generated datacontext class.
to sum up.. is there anyway to use both approaches on the same DAL? taking in mind modifying auto-generated classes is absolutely not an option.
Thanks!
I am using entity framework with database first approach, along with EF 4.x DBContext Generator. All my entity classes have navigation properties which are marked as "virtual". I want to remove virtual keyword from all my entity classes. There are around 350 entity classes
You must modify T4 template (.tt) file to remove virtual keyword.
Removing virtual will mean that you are unable to use lazy loading for entity framework. In my opinion lazy loading has a very limited scope and is mostly misused (which often causes performance issues). However do note that when you turn it off you may need to adjust your code to manually load additional navigation properties when you retrieve data. You can do this by using .Include in your EF query
To remove the virtual flag you probably need to disable lazy loading in your EF generator.
You may want to try this VS extension, It adds (among other things) fine control over the virtual modifier:
EF Designer Extender
I'm currently trying to migrate a project to EF4 POCOs in order to get rid of EntityObject in my business logic and ran into an issue with duplicate table names. The DAL has access to 3 different databases and there are 3 .edmx files, one for each database.
However, some tables in those databases share the same name, e.g. DB1.CUSTOMER and DB2.CUSTOMER. I managed to have the related entities created in different namespaces (one namespace for each database) like MyApp.Db1.CUSTOMER and MyApp.Db2.CUSTOMER, the trouble is EF can't decide which one to pick and claims there was an ambiguity which isn't actually the case.
Is there any way of mapping Entities to their respective POCOs manually or any sort of workaround? This is EF 4.2.
EF doesn't use namespace when recognizing entity type. The name of class is matched directly with name of entity in your model diagram (EDMX). So the workaround is using different names in different models which will also make your code much better readable.
I've been scratching my head over this for over a week now and haven't gotten anywhere :( We have an existing legacy DB that I'm trying to model my entities against. The tables are extremely bloated and we do not have enough bandwidth to create new, optimized tables. So I'm having to work with what we already have. However, I do not want to use all the redundant columns that are exposed by the DB. My initial plan was to use Views in my Model but that is looking to be equally hairy with very little documentation around.
Now, what would be the best way to go about creating a Model with just a select few columns? All I need is a bunch of read-only entities; so if there is a way to ignore non-nullable columns from the schema, I'd be all set. I was planning on making use of POCOs else I'd have to create my own mappings I reckon.
UPDATE: By POCOs, I mean I'd like to use the ADO.NET POCO Entity Generator.
What about creating views in the DB, and only importing the views into the model?
Well, if you need only a bunch of entities and if they won't change a lot during time, than I would just pick the tables you need, generate the entities with the normal wizard and all collumns, and than delete all not needed collumns manually in the model designer.
add the table to your EF, and just delete the properties you don't want. it just won't map those DB fields.