I currently try to migrate an application of mine from datasets to EF4.
So I have an existing database. One of the tables is mapped to 2 classes, depending of the value of a column.
So I am in a TBH schema with an existing table and an existing discriminator.
Is there a way to discribe this using annotations ?
thanks
No. TPH discriminator can be controlled only through Fluent-API.
Related
I want migrate the core data two models. According to apple documents there are two ways to migrate the model like light weight migration and mapping model. My confusion is In which scenario i need to use mapping model way of core data migration.
Lightweight migration is performed when you add/modify/delete attribute or entity - generally simple operations.
Mapping option is needed when you have to transform one schema into another.
Let's say you and entity A, but you need to divide it into two: B and C.
Then you have to tell CoreData how to map entities and attributes between the schemes.
This process is a little bit more complicated than lightweight migration. It requires to create mapping model file, define NSEntityMigrationPolicy subclass if needed, and so on, depending on your needs.
I'm using Entity Framework 4 and with a Database First binding, and EF is not generating the entities for a few of my tables. I'm not getting any errors, and no matter how many times I select the tables to generate from the "Update Model from Database" popup menu on the design surface, the same tables are still missing from the model.
I get no errors in the wizard. They just don't get generated. Any clues?
EF requires a primary key on the table. EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key. If one or more columns are not nullable, EF will evidently derive a primary key for the table.
EF will ignore table without primary keys.
Options I can think of:
Did you check the box next to those tables?
Did you previously add them, then delete their entities but keep the cache of the tables?
If so you can remove them from entity browser window and re-add them
or manually add entities and define the table they map to in mappings window.
Perhaps tables were classified as relations instead of entities?
You can manually add the entities and choose the table they map to in mappings window.
Actually, in my case, it doesn't work because I was using a hierarchyid field as a primary key and EF doesn't work with this field type, so, it didn't import the table, because a valid PK is required.
A possibility is when you're using tables with some different field types, as hierarchy in SQL Server.
Without Primary Key Tables where Skip Automatically on EF, OtherWise You Fix a Value as Not Null.
I want to create a hierarchical object model in ASP.NET MVC, but I'm not sure what would be the best way to design database for this. I have a Product base class with certain properties like Title, Price, OnHandQty etc. I have several inherited classes like Book, which has extra properties like ISBN number, Author etc. Many of my products will fall under generic (base) Product class, but some products will fall under these derived classes (e.g. Book). I am not sure what is the best methodology to map this to database. Should I create separate tables for each product type (including one for generic product)? Or is there any better way?
Please note that I'm not really asking about OR mapping. I know how to create classes from DB tables using Entity Framework. But in this case I am confused about the database design itself.
If you are going to use Entity Framework then you should check out Inheritance with EF Code First by mortezam. He explains three strategies that can be used for representing an inheritance hierarchy:
Table per Hierarchy (TPH): Enable
polymorphism by denormalizing the
SQL schema, and utilize a type
discriminator column that holds type
information.
Table per Type (TPT): Represent "is
a" (inheritance) relationships as
"has a" (foreign key) relationships.
Table per Concrete class (TPC):
Discard polymorphism and inheritance
relationships completely from the
SQL schema.
The idea (with Code First) is that you define your classes and inheritance and let the framework create the database for you. That way you don't need to worry so much about the database design.
You might also want to think about using an Object Database or one of the NoSQL storage strategies like Mongo DB which work better than relational databases when you have these kind of 'jagged' classes.
I'm an NHibernate developer trying to give Entity Framework a shot in a hobby project. I'm used to specifying mapping data in code using Fluent NHibernate. Pardoning Microsoft's belief that developers shouldn't be allowed to write code, I'm trying to create my mappings using the Entity Framework's visual designer surface (which you get by opening the .edmx file in Visual Studio).
I have no idea how to set up a many-to-many relationship! I have 'updated the model' from the database, but I get two one-to-many relationships with a new entity corresponding to the junction table (which contains only foreign keys and its own primary key).
So far, all attempts at working this out by clicking on the entities and relationships and such have failed. Can anyone give me a pointer?
Your junction table must be what MS calls a 'pure join table' - it must contain only the two foreign keys, and no other columns. In your case, that means you must delete the primary key column.
When you add the association to the model you choose each of your two tables and choose "many" on both sides of the relationship. When this generates the database script a join table with only the two keys will be created for you.
By the way: If you don't like using Model-first and would rather code research "Code-First" development for the Entity Framework. You can also do Database-First if you prefer that.
I have three databases, x, y, z. Let's assume MS can speak to all of them via odbc or something else.
When I was in webforms I would create a tableadapter and conduct a query. I could do this for each connection I had, so I had three queries.
I would drop each connection and dataset on my page. Each control I used would call the appropriate dataset and populate it's gridview or whatever. All was well. I had three databases, three hits, all on the same page, for one integrated page for the customer.
How can I do this same thing in ASP.NET MVC? Please.
Thank you.
You get your data from your databases and return all the results in your ViewModel
the simplest way would be to get it all in your controller, assign it to your model the send it to your view
Using ASP.Net MVC entity framework, create entity classes for each of the 3 databases (Here it's assumed that you are querying entirely different tables from 3 different databases). What you get here are 3 entity classes, each having it's own properties that directly correspond to the table column names you are retrieving. Now, you don't need to worry about 3 databases. Entity framework abstracts it into a set of properties that map into different tables in x,y and z databases you are retrieving from.