我想知道使用EF4 Code-First方法的优缺点.我们可以复制EF4生成的类提供的所有功能,如延迟加载,加载相关实体等吗?
谢谢
如何将带有EF4的SQL CE 4.0部署到ASP.NET MVC 2.0的共享托管提供程序?
我已经在我的bin文件夹中包含了System.Data.SqlServerCe.dll和amd64 + x86目录,但仍然没有找到".net提供程序".我意识到它目前在CTP中,但这仅用于测试目的.我的项目+主机配置为.net 4.0
deployment asp.net-mvc sql-server-ce entity-framework-4 asp.net-mvc-2
我在Accounts和PaymentSystems之间有多对多的关系.我想列出尚未分配到帐户的所有PaymentSystems.为此,我尝试使用以下LINQ to Entities查询:
PaymentGatewayEntities pge = new PaymentGatewayEntities();
Account account = pge.Accounts.Single(item => item.id == accountId);
var paymentSystems = pge.PaymentSystems.Except(account.PaymentSystems);
Run Code Online (Sandbox Code Playgroud)
但是,在尝试显示结果时出现以下异常:"System.NotSupportedException:无法创建类型为'MyNamespace.Models.PaymentSystem'的常量值.只有基本类型('如Int32,String和Guid')在这种情况下得到支持." 我究竟做错了什么?我正在使用EF4.
UPD:var paymentSystems = pge.PaymentSystems.Where(item =>!item.Accounts.Contains(account))也会产生相同的异常.
我找到了一些如何使用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
使用一个优于另一个有什么好处?我知道POCO课程更优化,但是他们是否值得过度杀伤?我们是应该总是使用POCO还是有时候你应该更喜欢实体框架类?
如果我想从MSSQL表中检索20到39或40到59的记录.
由于我正在使用MVC和EF4,如果我只是执行Linq查询和Skip()以及Take()过程来请求分页,那么性能是否相同....或者在GetList()存储过程中执行它是否更好本身?
我最近重构了一个WPF应用程序,以便它不再包含DbContext在using子句中的每次使用(请参阅此问题).相反,我的应用程序只使用相同的DbContext单例.
除了一个小问题外,这很有用.我有一个例程,从头开始重建数据库并插入一些默认数据.此例程直接使用ADO.NET(而不是DbContext),因此DbContext不知道数据库现在完全不同.
有没有一种方法来重置DbContext而不处理它?如果可能的话,我想避免处置,因为这会破坏整个应用程序中对原始单例的几个引用.
在我的应用程序中,我有以下代码......
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) 这使我今天无法解决问题.我有这个简单的查询
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 ×3
c# ×3
asp.net-mvc ×1
code-first ×1
deployment ×1
hierarchy ×1
inheritance ×1
lambda ×1
poco ×1
t-sql ×1
unit-of-work ×1