two tables considered as one - c#

I have two tables like this:
Table1(id, name)
Table2(id_of_table_1, code)
I don't need an entity for Table1 or Table2, but one entity for both together:
class Merge{
public virtual long id{get;set;}
public virtual string name{get;set;}
public virtual string code{get;set;}
}
How can I load the tables to the edmx so that they will considered as one?
I don't have any control on the database and I can't create tables or views.

You are looking for advanced mapping called Entity splitting.

I think this is what you're lookig for: How to: Define a Model with a Single Entity Mapped to Two Tables

In short, you need to do this:
Add the two tables as two separate entities to your model
Cut the scalar values from the Table2 entity to the Table1 entity
Delete the Table2 entity
In the Table Mapping options of the Table1 entity, map the Table2-fields to Table2
For a more detailed explanation, you can have a look at this blog post.

Create a function inside your Merge class that writes the properties of your class to the appropriate Table1 and Table2 EDMX objects. Your Merger class should have references to those EDMX objects as internal variables. So this class is like a wrapper for your 2 table objects.

Related

EntityFramework custom many to many without navigation property

I want to do many-to-many relation between these objects:
class Flyer {
Guid Id;
virtual ICollection<Address> Stores;
}
And:
class Address {
Guid Id;
}
Of course these models are simplified.
And I do not want a navigation property to Flyer inside Address because Address is in relation with other objects too. It is possible?
Fluent API only
How are you identifying what the address belongs to if it can belong to multiple different tables? Do you have a Join table? Like a AddressToFlyer Table that establishes the relationship between the two tables?
When I have complex queries that need to take place like this where one table can be used by many tables, I create join tables and typically create views that handle the joins and give me the data what I want so I can just do a select in Entity Framework and not worry about the Navigational property, then implement said view in Entity framework like you would a regular table.

EF generated code inheritance

What do I need to do in my database if I want my EF generated classes to look like this:
class A , class B : A , class C : A ?
Currently my DB tables look like this:
table A ID PK
table B ID PK and FK referencing table A ID
table C ID PK and FK referencing table A ID
thanks in advance
You need to specify this when generating your classes. Basically you choose a BaseType in the designer for class B and class C and remove the association. Here is an explanation how to do that (may differ slightly with different versions of EF)
http://msdn.microsoft.com/en-us/data/jj618293.aspx
What you want to do is known as "table per type" in case you want to look for more details online.

Linq Data Mapping

I have an old system with a lot of stored procedures that I am redeveloping using C# and MVC. I created Entities base on the system and I use Entity Framework as ORM. What is the best practice to map output of an stored procedure that includes some joins to my entities.
For example I have table1, table2 and table3 then I have and stored procedure includes:
Select * from table1 join table2 on ...
join table3 on ..
then I want the entities be filled by this data and in my controllers I use linq for accessing this data
As I have this three table I created three entities:
class table1{
public int table1Id;
public String value{get;set;}
public IEnumarable<table2> table2List{get;set;}
}
class table2{
public int table2Id{get;set;}
public String table2value{get;set;}
public IEnumarable<table3> table3List{get;set;}
}
I just added some pseudo code to make it clear. I want to map output of stored procedure to these entities.

Using same entity in two models

I am making an library which will be used to talk with database using Entity framework. For different workflows I need different tables from database. So I decided to have separate models for separate workflows. But for some workflows one entity is used in multiple models. Now for one model I have modified my entity class (changed some getters/setters and added custom functions). But when I create new model for different workflow then model will generate entity with default names. I have to edit it again and code will be duplicated. Both are in different namespaces (one is Model1Namespace, second is Model2Namespace).
So what I exactly need is that if entity is used in different classes a single code is used (no duplicate code). What are the best practices? Do EF provide us something or we need to implement it ourself?
Example:
Database tables: TableA, TableB, TableC, TableD
Models: Model1 -> TableA, TableB
Model2 -> TableA, TableC,
Model3 -> TableC, TableD
Edit:
I have a database containing 4 tables (TableA, TableB, TableC, TableD). I create a Entity data model of the database which contains TableA and TableB. In entity designer view I modified names of properties of TableA Entity so that they are readable. Now I create another model which contains TableA and TableC. Now here I have to rename all properties of TableA again. Now this is repeat work. Now if I add some custom action to my Entity for Model1 then I have to write (copy) them to new Model2 Entity as well. I need to avoid this. As I really don't know how many models I will create. And if I have to do this stuff again and again then it will take lot of time.

Using existing database and EF code-first, mapping a lookup table to entity

I am using entity framework, code first, 4.0, hitting an existing legacy database for read-only access. The database is normalized, so
Table [Event]
[ID]
[Date_Entered]
[Event_Status_Key]
Table [Event_Status]
[Event_Status_Key]
[Event_Status_Description]
My class looks like
public class Event
{
public DateTime DateEntered { get; set; }
public string StatusDescription { get; set; }
}
This is a WCF service layer application.
My two questions:
Is there any easy way to populate the status description without creating a second Dictionary-type object? I've seen questions like this: Entity Framework Mapping to Lookup table, but they seem to be focused on object to object, and I really just want a primitive. I'd prefer using the fluent API as opposed to attributes.
When the data is loaded, is any of the data cached at the code layer? Or does each check on the StatusDescription mean a separate call on the [Event_Status] table?
Edit: A possible (more subjective, which is why I didn't bring it up) third question is how close should the data entities match the database. Is it always a one-to-one field/table? Is what I'm doing (joining two tables into one data entity obejct) bad?
Thanks.
Entity framework expects that you will map both tables as separate entities and use projection in your query:
var query = from e in context.Events
select new WcfEvent // Your original Event class is just DTO
{
DateEntered = e.DateEntered,
StatusDescription = e.EventStatus.EventStatusDescription
};
This example expects correctly one-to-one mapping of your Event and Event_Status tables.
If you need any kind of caching you will have to implement it yourselves. Projected results are not even tracked by the context.

Categories

Resources