Entity Framework - seemingly simple question
Long time listener, first time caller...
So I'm converting an existing app over to ef. I am enjoying the experience so far. I have a POCO class that has some simple user properties.
ex.
public string FirstName { get; set; }
public string LastName { get; set; }
other properties...
public string FullName
{
get { return FirstName + " " + LastName; }
}
How do I accomplish this with EF? Can is be done in the EDM, or do I have to create a par开发者_StackOverflowtial class? I prefer the EDM...
You need partial class because this is property computed in your code not in database. Problem here is that read/write entity must have only properties mapped to a table but your table probably doesn't have FullName
column = you can't map it and you can't use it in queries. Computed properties in EDM are possible but only if you have read only entity defined by Query view (view defined in ESQL) or Defining query (custom SQL select). If you want to use FullName
in query you must define it as model defined function. Query view, Defining Query and model defined functions are advanced EF features which are not available in designer. You must open EDMX as XML and edit it manually.
If you already have a POCO architecture you may want to consider using EF4 CodeFirst. It allows you to use your existing POCO classes "as-is", no rework of things like computed properties required, and you just wire them up to a DBContext that you configure in code.
The approach is pretty attractive to most POCO folks and it's made me give EF a second look. Scot Gu introduces it in this post http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx and he's done a series of articles since that give lot's of detail on how to configure persistence with CodeFirst.
精彩评论