可能重复:
在.NET 3.5 SP1上使用EF 4
是否可以在.NET 3.5应用程序中使用Entity Framework v4.0?我的意思是,我可以引用EF 4.0的相关程序集并在我的.NET 3.5应用程序中使用它吗?
谢谢.
长期以来,我们一直希望创建一个不会丢失历史的案例管理系统.当进行更改时,我们希望记录该更改,但能够返回到任何时间点并查看记录的外观.我想向Stack Overflow社区提出这个问题,看看有什么方法可以做到这一点,是否已经有技术来实现这一目标?
我找到了一些如何使用ef4创建工作单元的示例,我没有使用di/ioc,我想保持简单,这是一个例子(90%的灵感),我认为它没关系但是因为我正在看从现在开始使用的模式我想最后一次提出意见.
public interface IUnitOfWork
{
void Save();
}
public partial class TemplateEntities : ObjectContext, IUnitOfWork
{
....
public void Save()
{
SaveChanges();
}
}
public interface IUserRepository
{
User GetUser(string username);
string GetUserNameByEmail(string email);
void AddUser(User userToAdd);
void UpdateUser(User userToUpdate);
void DeleteUser(User userToDelete);
//some other
}
public class UserRepository : IUserRepository, IDisposable
{
public TemplateEntities ctx;
public UserRepository(IUnitOfWork unit)
{
ctx = unit as TemplateEntities;
}
public User GetUser(string username)
{
return (from u in ctx.Users
where u.UserName == username …Run Code Online (Sandbox Code Playgroud) 我试图理解EF4中的继承映射.
我的数据库有两个表,结构如下:
PersonCategory表:
人员表
PersonCategory表有四个条目,每个条目代表一个类别 - Student,CourseInstructor,Staff和Advisor.
从在线阅读文章,我认为Table Per Hierarchy将是适用于此场景的模型.所以在EF4中,我创建了四个实体(Student,CourseInstructor,Staff和Advisor),每个实体都继承自Person表.然后我将它们中的每一个映射到Person表并向每个表添加一个条件(例如,StudentID = 1表示Student实体,CategoryID = 2表示Staff实体)以区别于其他表.我还从Person表中删除了CategoryID属性并使其成为抽象类.但是我收到以下错误,因为我从Person表中删除了CategoryId属性.
错误3015:从第101,108,114,120,126,133行开始映射片段时出现问题:从表Person(CategoryID)到表PersonCategory(CategoryID)的外键约束'FK_Person_PersonCategory'::映射不足:必须映射外键一些参与概念方面的外键关联的AssociationSet或EntitySets.
Table Per Hierarchy是适用于此场景的模型吗?如果没有,那么我应该如何在EF4中处理这种情况?
.net inheritance hierarchy entity-framework-4 table-per-hierarchy
如果我想从MSSQL表中检索20到39或40到59的记录.
由于我正在使用MVC和EF4,如果我只是执行Linq查询和Skip()以及Take()过程来请求分页,那么性能是否相同....或者在GetList()存储过程中执行它是否更好本身?
在我的应用程序中,我有以下代码......
public Boolean SaveUserInformation(UserInfoDTO UserInformation)
{
return dataManager.SaveUserInfo(new UserInfo()
{
UserInfoID = UserInformation.UserInfoID.HasValue ? UserInformation.UserInfoID.Value : 0,
UserID = UserInformation.UserID,
ProxyUsername = UserInformation.ProxyUsername,
Email = UserInformation.Email,
Status = UserInformation.Status
});
}
Run Code Online (Sandbox Code Playgroud)
此代码调用使用实体框架的dataManager对象上的方法...
public Boolean SaveUserInfo(UserInfo userInfo)
{
try
{
//Validate data prior to database update
if (userInfo.UserID == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with UserID property set to NULL."); }
if (userInfo.ProxyUsername == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with ProxyUsername property set …Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework 4.0开发一个应用程序.和WCF.
我正在返回对象列表说(员工)
并说该对象的导航属性(部门)
和部门还有一个航海财产(分公司)
我把一切都包括在内
Employees.include("Departments.Branch");
Run Code Online (Sandbox Code Playgroud)
现在发布所有那些Branch相同的部门,在WCF上反序列化时设置为null(第一个除外).
我需要使用分支进行一些绑定,请指导我如何摆脱这个问题.
这是实体的屏幕截图 
我正在研究动态数据.
在创建动态模型并在global.asax中注册之后
DefaultModel.RegisterContext(typeof(masterEntities1),new ContextConfiguration() { ScaffoldAllTables = true });
Run Code Online (Sandbox Code Playgroud)
当我运行一个应用程序时,它会显示一个表列表但是当我点击任何一个表时它会抛出一个异常:
只有LINQ to Entities中的排序输入才支持'Skip'方法.必须在方法'Skip'之前调用'OrderBy'方法.
但我没有在我的申请中声明任何查询.
由于我的数据库是使用德语表和列名设计的,因此实体框架的默认复数功能对我不起作用.
我已经找到了几个讨论这个问题的资源,但它们似乎都没有用.
我发现:有PluralisationService,我可以添加映射:
PluralizationService pluralizer =
PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
ICustomPluralizationMapping mapping = ps as ICustomPluralizationMapping;
mapping.AddWord("Tabelle", "Tabellen");
Run Code Online (Sandbox Code Playgroud)
但下一步是什么?我试过:
EntityModelSchemaGenerator generator = new EntityModelSchemaGenerator(container);
generator.PluralizationService = pluralizer;
generator.GenerateMetadata();
Run Code Online (Sandbox Code Playgroud)
并将它们都放在我的POCO T4模板中.但它引发了以下异常:
EntityContainer'ContainerName'不是商店EntityContainer.参数名称:StoreEntityContainer
at System.Data.Entity.Design.EntityModelSchemaGenerator.Initialize(...)
at Microsoft.VisualStudio.TextTemplating ... GeneratedTextTransformation.TransformText()
entity-framework poco pluralize entity-framework-4 entity-framework-5
这使我今天无法解决问题.我有这个简单的查询
var result =
DataContext.Accommodations.Where(a =>
(criteria.MinPrice == null || a.AccommodationRates.Any(r => r.From >= criteria.MinPrice)) &&
(criteria.MaxPrice == null || a.AccommodationRates.Any(r => r.To <= criteria.MaxPrice)) &&
(criteria.Locations == null || criteria.Locations.Count == 0 || a.AccommodationPlaceJoins.Any(j => criteria.Locations.Contains(j.Place.PlaceName)))
);
Run Code Online (Sandbox Code Playgroud)
此查询的最后一行导致我出现问题
(criteria.Locations == null ||
criteria.Locations.Count == 0 ||
a.AccommodationPlaceJoins.Any(j => criteria.Locations.Contains(j.Place.PlaceName)))
Run Code Online (Sandbox Code Playgroud)
它给出的错误是
无法创建类型为'System.Collections.Generic.IList`1'的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').
我甚至没有尝试创建列表.我在这里尝试做的就是带回与一个地方相关的住宿(Place表中通过AccommodationPlaceJoin表链接到住宿表的地名)等于标准中的任何一个地名.Locations(属于IList类型).
我已经尝试将此行更改为此,但它不起作用.
(criteria.Locations == null ||
criteria.Locations.Count == 0 ||
a.AccommodationPlaceJoins.Any(j => criteria.Locations.Any(l => l == j.Place.PlaceName)))
Run Code Online (Sandbox Code Playgroud) .net ×4
c# ×4
.net-3.5 ×1
.net-4.0 ×1
dynamic-data ×1
exception ×1
hierarchy ×1
inheritance ×1
lambda ×1
pluralize ×1
poco ×1
sql ×1
t-sql ×1
unit-of-work ×1
wcf ×1