2015-03-19sql
1.当项目框架为.Net Framework4.0的时候,使用EF6.0会出问题。数据库
解决方法:将引用的EF相关dll改为EF5.0的DLL。服务器
2.EF使用Model First方式创建数据库时,发布网站至IIS或者服务器上时,微软会采起sql登陆验证而采起Windows(即链接字符串为Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=true; 时会出问题)框架
解决方法:将EF的链接字符串改成ide
1 <add name="Model1Container" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=./;Initial Catalog=kashishop;User Id=kashishop;Password=123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
3.使用EF,发布网站时,要在配置文件中添加字体
1 <configSections> 2 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 3 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 4 </configSections>
和网站
1 <entityFramework> 2 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 3 <parameters> 4 <parameter value="v11.0" /> 5 </parameters> 6 </defaultConnectionFactory> 7 </entityFramework>
2015-03-21ui
1 GoodService gs = new GoodService(); 2 Good g = gs.LoadEntities(u => u.Id == id).FirstOrDefault(); 3 g.Name = name; 4 g.Price = double.Parse(price); 5 g.Quantifier = quantifier; 6 g.Instruction = instruction; 7 g.TypeId = typeId; 8 gs.Update(g);
把Service提早,用同一个Service进行取值和更新操做就没问题。spa
可是像下面这样,用不一样的Service对象对同一个实体的导航对象进行更新就会报此错误。code
1 Good g = new GoodService().LoadEntities(u => u.Id == id).FirstOrDefault(); 2 g.Name = name; 3 g.Price = double.Parse(price); 4 g.Quantifier = quantifier; 5 g.Instruction = instruction; 6 g.TypeId = typeId; 7 GoodService gs = new GoodService(); 8 gs.Update(g);
使用EF更新数据时,若是要更新的对象有相关的对象(换句话说,就是要更新的表有主外键关系,或者导航关系),这些对象必须来自同一个IEntityChangeTracker 。