Entity Framework DB First - Naming Navigation properties - entity-framework

I am using Entity framework v 6.1.1 in my application.
My database has 2 tables User, Location.
User table
-----------
UserID
HomeCityId(FK -> LocationId)
CurrentCityId(FK -> LocationId)
Location table
LocationId
LocationName
Using DB First approach, I created a Entity data model for these two tables.
The generated entity class of User table
public int UserId;
public int HomeCityId;
public int CurrentCityId;
public virtual Location Location { get; set; }
public virtual Location Location1 { get; set; }
Is there a way to name these virtual properties as HomeCity and CurrentCity instead of Location and Location1?

In case of Entity Framework Database First Approach, using MVC, Entity Framework, and ASP.NET Scaffolding, you can create a web application that provides an interface to an existing database. The automatically generate code enables users to display, edit, create, and delete data that resides in a database table. The generated code corresponds to the columns in the database table.
To add Data Annotations to the data model to specify validation requirements and display formatting-
changes to the auto generated class by Entity Framework is not recommended, as it is sure to get overridden whenever you update your EDMX data model, so metadata & partial class.cs files are created.
Also, note that all the files- EDMX , PartialClasses.cs and
Metadata.cs should reside at same location in your application
directory.
Please refer this tutorial:
Enhance data validation and display formatting for EF Database First with ASP.NET MVC app

Related

Entity Framework Database First - Map to Generic List

I'm using Entity Framework - Database First in my project. My model has a view with only one VARCHAR column:
CREATE VIEW MyView
AS
SELECT 'Eris' Eris
FROM MyTable
By default, this view gets mapped to its own entity with just one property:
public virtual DbSet<MyView> MyViews { get; set; }
How can I change this so that the view gets mapped to a List of strings instead:
public virtual List<string> Eris { get; set; }
Unfortunately EF does not support mapping collections of value types. If you really want to implement this scenario then you might want to look into other ORM frameworks that have this feature like NHibernate.
If that's not an option and you have to stick to EF then you're forced to create an entity with one property like you mentioned in your question.
The entity model represents one element in the table.
When you retrieve data from the table, you will get a list of entity model objects.

Add child node to existing parent Entity Framework

Let s say i have two classes
[Table("User")]
public class User
{
public string Name { get; set; }
public string Surname { get; set; }
}
[Table("Manager ")]
public class Manager : User
{
public int Title {get;set;}
}
and i m using entity framework 6.1.2 and table per type approach for saving entity.
Now i want to add a child (i.e. Manager) but there is a parent(i.e. User) for this child.
so what should i do
how do i insert only the child node.
You are mixing some OO principles.
A manager is a user.
This means that if you add a manager to system, you are effectively also adding a user. You can add a user to the system if it is not a manager.
Adding a manager will update both user table and manager table. Adding a user that is not a manager will only add an entry in the user table.
So in summary. All users both normal and managers will appear in the users table. But for the users that are also manager , there will also be a record in the manager table. The information that belongs to a manager is spread over 2 tables in the database. In EF because you have used inheritance you are using only a manager instance, but because it is derived from user, you get access to the user properties as well. Relational concepts and OO concepts are not the same, EF does the mapping between these distinct concepts for you, hence the name Object Relational Mapping.

Can I lazy load a navigation property by delegating to a stored procedure in EF?

I have the following customer class:
public class Customer
{
public long Id { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
My database has Customers and Orders tables, but no foreign key relationships. Orders for a customer are obtained using a stored procedure that takes the customer ID and returns the order rows. I can't modify the database.
I know how to call the stored procedure from Entity Framework, but, is it possible to configure the DbContext using the fluent API so that accessing the Orders collection of a customer object would lazy load the entities via a call to the stored procedure?
I'm using the latest version of EF.
No, you can't. Lazy loading is coded in the proxy object that EF creates (if possible), there's no way to intercept/configure the way proxies are generated.
It's not even possible to map the default read action of a DbSet to a stored procedure. It's always a query. Only create, update and delete can be mapped to stored procedures.
The reason (I think) is that stored procedures are not composable, so if in a complex LINQ query one entity would be mapped to a stored procedure (for reading) it wouldn't be possible to turn the query into one SQL statement.

How to couple 2 objects using the highest row version of one of the objects

I am having a look at Entity Framework and am wondering how to join 2 objects where one object contains a row version column.
For example
public class Product
{
[Key, Column(Order = 1)]
public Guid ProductGuid {get;set;}
[Key, Column(Order = 2)]
public int RowVersion {get;set;}
public string DisplayName {get;set;}
// ... more properties ...
}
public class DeviceConfiguration
{
[Key]
public Product TheProduct {get;set;}
[Key]
public string WorkstationName {get;set;}
public string ConfigurationString {get;set;}
}
How can I get DeviceConfiguration to pick up the Product row with the highest value of RowVersion?
From what you described, you already have a database and you would like to develop an application by using that database. Here's an excellent resource about working with the EF which also covers the scenario you are currently facing:
Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)
Take a look at the Entity Framework Development Approaches sub-chapter, probably you'll be interested to know more details about the:
Database First
If you already have a database, the Entity Framework can automatically
generate a data model that consists of classes and properties that
correspond to existing database objects such as tables and columns.
The information about your database structure (store schema), your
data model (conceptual model), and the mapping between them is stored
in XML in an .edmx file. Visual Studio provides the Entity Framework
designer, which is a graphical designer that you can use to display
and edit the .edmx file. The sections Getting Started With the Entity Framework and Continuing With the Entity Framework in the Web Forms
tutorial series use Database First development.

Entity Framework 4 with Existing Domain Model

Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4.
I have a project containing the domain model (pocos) which I was using for nHibernate mappings. Ive read in blogs that it is possible to use my existing domain model with EF4 but ive seen no examples of it. Ive seen examples of T4 code generation with EF4 but havent come accross an example which shows how to use existing domain model objects with EF4. Im a newby with EF4 and would like to see some samples on how to get this done.
Thanks
Aiyaz
Quick walkthrough :
Create an entity data model (.edmx) in Visual Studio, and clear the "custom tool" property of the edmx file to prevent code generation
Create the entities in your entity data model with the same names as your domain classes. The entity properties should also have the same names and types as in the domain classes
Create a class inherited from ObjectContext to expose the entities (typically in the same project as the .edmx file)
In that class, create a property of type ObjectSet<TEntity> for each of you entities
Sample code :
public class SalesContext : ObjectContext
{
public SalesContext(string connectionString, string defaultContainerName)
: base(connectionString, defaultContainerName)
{
this.Customers = CreateObjectSet<Customer>();
this.Products = CreateObjectSet<Product>();
this.Orders = CreateObjectSet<Order>();
this.OrderDetails = CreateObjectSet<OrderDetail>();
}
public ObjectSet<Customer> Customers { get; private set; }
public ObjectSet<Product> Products { get; private set; }
public ObjectSet<Order> Orders { get; private set; }
public ObjectSet<OrderDetail> OrderDetails { get; private set; }
}
That's about it...
Important notice : if you use the automatic proxy creation for change tracking (ContextOptions.ProxyCreationEnabled, which is true by default), the properties of your domain classes must be virtual. This is necessary because the proxies generated by EF 4.0 will override them to implement change tracking.
If you don't want to use automatic proxy creation, you will need to handle change tracking yourself. See this MSDN page for details