Entity Framework 2 different tables retrieve data as single entity - linq

We have 2 exactly same tables in our database. One is an archive of the other.
How can I retrieve the data from the archive table without changing the entity type using Entity Framework and Linq?

When you map your tables to EF entities you can automatically create classes for the entities. Why do you want to avoid creating separate classes for different tables? If you want a common contract for the two so that you can pass them to other functions that perform the same operations on both, I would add an interface to the generated entities instead. That way you can use the auto generated entities but still be able to treat them as "one"

You can always do a .Select(x=>new SomeTypeOfObject{Prop1 = x.Prop1, Prop2 = x.Prop2})

Related

Querying multiple tables using jpa repository

Suppose if I have 3 entities - User, Skills, Department
and I have repositories corresponding to all of them - UserRepository, SkillRepository, DepartmentRepository.
I understand that the relation mapping between entities i.e. one-one many-many should be specified in the respective entity classes. The question is I want to use all of the 3 entities in a query. How would I do it? A single repository is associated with only one entity right? So, how/where would I write it?
As there are many different ways to specify queries with Spring Data JPA there are various answers to this.
Maybe you don't have to. If entity A references B and you just want to get the Bs with your A you simply use your ARepository to load As and use object navigation to get your Bs. You might read up on eager and lazy loading for more information about how to control this.
If you want referenced entities in the where condition you can use property paths in your query method names: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions
If you are using #Query annotations you can do (almost) whatever you want with JPQL. Among others, you may as well navigate properties to use them in where clauses.
In general, you'd put that query in the matching repository based on the primary entity returned.

Very simple dillema that has been killing me scaffolding in mvc 3

Summary of the question: How do i scaffold two or more tables with linq to entities.
I cant find an example; they always scaffold only one table.
Details:
If I have two tables and I use LINQ to entities with a t4 template for dbcontext capability like such:
Table1
Name LastName PositionId
Jose j 1
Table2
PositionPrimaryKey PositionId PositionDescription
1 1 MainProgrammer
If I had these table mapped with linq to entities how would I scaffold them?
Then i put Table1 as my Model class.
I have my employeesentities as dbcontext
But that only creates the values for table 1 and not 2.
If I create a new model that contains both entities, it says is not part of Employeeentities and the class could not be modfied to add my new entity.
So, you have a 1:1 relationship between these tables? If yes, I suggest you creating an entity by hand, set its defining query to the necessary join, and map Insert/Update/Delete to stored procedures in the Mapping Details screen. It involves some (quite simple) sql, but it is the cleanest way for your code above.
If it's not a 1:1 relationship, you need to modify the t4 template to conditionally create the fields of the linked property (it has to navigate the property, and based on some condition, like you say "if property is called Table2", create the extra fields). If you have already done so and it doesn't work, maybe there's something going on with the selection of properties used by MVC scaffolding. It might use reflection and choose only primitive types.

Name conflicts in Entity Framework using database schemas?

I have these two tables in my database:
client.Employee
employee.Employee
When I try to import this into entity framework I get two table objects created:
Employee
Employee1
Is there a way to handle naming conflicts that will work better than this? And really, I would prefer that my schema is represented some how for non conflicting tables as well.
Unfortunately no. Information about schema is only included in storage description (SSDL) and it is not passed to conceptual model (CSDL) so in conceptual model you have two entities named Employee and EF is using the most basic way to resolve that. Another problem is that this probably cannot be modified because generating model from database is not driven by any T4 template which can be changed whereas reverse processing (generating SQL database creation script from model) is.

Linq To SQL : Modeling Associations

I have three tables Projects, Users and ProjectMembers. The ProjectMembers table is a mapping table and has only two columns ProjectId and UserId.
In my object model i have two classes Project and User. The Project class has a property IEnumerable<User> Members
I am using an external xml map file for mapping linq to sql associations. I am able to get the Project and the User data but I dont know how to map the Members association.
This sounds like a Many-to-Many mapping (Projects <-> Users).
In which case you are going to run into problems using Linq To SQL. To cut a long story short it does not really support that mapping. There are several workarounds which you can find on google, one of which is altering the partial class to provide the access to the Members/Projects collection on the Project and User classes respectively.
e.g. http://www.iaingalloway.com/2015/06/many-to-many-relationships-in-linq-to-sql.html

.NET 3.5 Linq Datasource and Joins

Have been trying out the new Dynamic Data site create tool that shipped with .NET 3.5. The tool uses LINQ Datasources to get the data from the database using a .dmbl context file for a reference. I am interseted in customizing a data grid but I need to show data from more than one table. Does anyone know how to do this using the LINQ Datasource object?
If the tables are connected by a foreign key, you can easily reference both tables as they will be joined by linq automatically (you can see easily if you look in your dbml and there is an arrow connecting the tables) - if not, see if you can add one.
To do that, you can just use something like this:
<%# Bind("unit1.unit_name") %>
Where in the table, 'unit' has a foreign key that references another table and you pull that 'unit's property of 'unit_name'
I hope that makes sense.
(EDIT misunderstood the question, revising my answer to the following)
Your LinqDataSource could point to a view, which allows you to overcome the problem of not being able to express a Join in the actual element. From "How to: Create LINQ to SQL Classes Mapped to Tables and Views (O/R Designer)":
The O/R Designer is a simple object relational mapper because it supports only 1:1 mapping relationships. In other words, an entity class can have only a 1:1 mapping relationship with a database table or view. Complex mapping, such as mapping an entity class to multiple tables, is not supported. However, you can map an entity class to a view that joins multiple related tables.
You cannot put more than one object/datasource on a datagrid. You will have to build a single ConceptObject that combines the exposed properties of the part Entities. Try to use DB -> L2S Entities -> ConceptObject. You must be very contrived if the DB model matches the ConceptObject field-for-field.
You are best using a ObjectDataSource when you wnt to do more complex Linq and bind your Grid to the ObjectDataSource.
You do however need to watch out for Anonymous types that could give you some trouble, but anything is posible...

Resources