Entity Framework用于简历数据库与逻辑对象的映射
假设我们有表:
CREATE TABLE [dbo].[stocks_EF](
[ID] int identity(1,1),
[Symbol] [varchar](10) NOT NULL,
[Price] [float] NULL,
[PricePct] [float] NULL,
[Volume] [int] NULL,
[AvgVolume] [int] NULL,
[MktCap] [varchar](20) NULL,
[Yield] [float] NULL,
[PE] [float] NULL,
[TradeDate] [datetime]
NULL,
[InsertTime] [datetime]
default(getdate())
)
Visual studio 2012的设置:
连接数据库:
add sql server connection
选择数据库:
到这里,我们已经把数据库内置到了visual studio 2012中了。
下面我们加入entity framework:
加入.edm,这个可以数据库命名
选择Generate from database,然后选择要创建model的database表:
完成后,我们看到生成的代码结构如下:
双击ModelDB.edmx,会看到一个图形化表,如果以txt打开它,会看到它的内在代码:分为三部分:StorageModels (db table), ConceptualModels (C# class),Mappings。
运行如下C#代码:
static void Main(string[] args)
{
using (ModelEntities context
= new ModelEntities())
{
context.stocks_EF.Add(new stocks_EF() { Symbol = "CAT", Price = 98.42, PricePct = 2.5, Volume = 29389, AvgVolume = 39392, MktCap = "56.5B", Yield = 2.2, PE = 10.2, TradeDate = Convert.ToDateTime("2013-06-17 00:00:00.000") });
context.SaveChanges();
}
}
会得到如下错误:
Unable to update the EntitySet 'stocks_EF' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
如果表有primary key,不用手动修改edmx文件,否则就要做改动:
这是因为该表没有primary key,有两个解决方案:
1. 写SP
2. 手动改.edmx的代码:
去掉store:Schema中的store,还去掉definingQuery部分即可
<edmx:StorageModels>
<Schema Namespace="modelModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityContainer Name="modelModelStoreContainer">
<EntitySet Name="stocks_EF" EntityType="modelModel.Store.stocks_EF" store:Type="Tables" store:Schema="dbo" store:Name="stocks_EF">
<DefiningQuery>SELECT
[stocks_EF].[ID] AS [ID],
[stocks_EF].[Symbol] AS [Symbol],
[stocks_EF].[Price] AS [Price],
[stocks_EF].[PricePct] AS [PricePct],
[stocks_EF].[Volume] AS [Volume],
[stocks_EF].[AvgVolume] AS [AvgVolume],
[stocks_EF].[MktCap] AS [MktCap],
[stocks_EF].[Yield] AS [Yield],
[stocks_EF].[PE] AS [PE],
[stocks_EF].[TradeDate] AS [TradeDate],
[stocks_EF].[InsertTime] AS [InsertTime]
FROM [dbo].[stocks_EF] AS [stocks_EF]</DefiningQuery>
</EntitySet>
</EntityContainer>
<!--Errors
Found During Generation:
warning 6002: The table/view
'model.dbo.stocks_EF' does not have a primary key defined. The key has been
inferred and the definition was created as a read-only table/view.
-->
<EntityType Name="stocks_EF">
<Key>
<PropertyRef Name="ID" />
<PropertyRef Name="Symbol" />
</Key>
<Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Symbol" Type="varchar" Nullable="false" MaxLength="10" />
<Property Name="Price" Type="float" />
<Property Name="PricePct" Type="float" />
<Property Name="Volume" Type="int" />
<Property Name="AvgVolume" Type="int" />
<Property Name="MktCap" Type="varchar" MaxLength="20" />
<Property Name="Yield" Type="float" />
<Property Name="PE" Type="float" />
<Property Name="TradeDate" Type="datetime" />
<Property Name="InsertTime" Type="datetime" StoreGeneratedPattern="Computed"/>
</EntityType>
</Schema>
</edmx:StorageModels>
运行代码后,发现insertTime没有输入,显然是DEFAULT contraint没有设置好:
只要加入黄色部分代码StoreGeneratedPattern即可.。
Refer to http://blogs.msdn.com/b/alexj/archive/2009/09/01/tip-34-how-to-work-with-updatable-views.aspx