我有一个使用接口的部分类,因为我无法继承原始抽象类,因为其他部分类是从Entity Framework 4自动生成的,因此已经继承了ObjectContext.
我的部分课程有以下内容:
namespace Model
{
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Utilities.BusinessRules;
using Utilities.BusinessRules.Rules;
[HasSelfValidation]
public partial class MyObject : IBusinessObject
{
private readonly IBusinessObject businessObject = new BusinessObject();
private IBusinessObject BusinessObject
{
get
{
return businessObject;
}
}
public Comment()
{
AddRule(new ValidateRequired("Title"));
}
public void AddRule(BusinessRule rule)
{
BusinessObject.AddRule(rule);
}
[SelfValidation]
public void Validate(ValidationResults results)
{
BusinessObject.Validate(results);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是界面:
namespace Utilities.BusinessRules
{
using Microsoft.Practices.EnterpriseLibrary.Validation;
public interface IBusinessObject
{
void AddRule(BusinessRule rule);
void Validate(ValidationResults results);
} …Run Code Online (Sandbox Code Playgroud) 我使用Entity Framework 4并遇到以下问题,即在环境事务中执行存储过程.这是代码:
public void UpdateOrderRequest(IOrder order, int requestId, int userId, Fee fee)
{
using (var tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
_storedProcedureDA.UpdateOrderRequest(requestId, userId, data.ClientId, data.RequestStatus, data.Date,
data.Type, data.Side, data.Quantity, data.ExecInst, data.Price,
data.StopPrice, data.TimeInForce, data.Description, data.Target);
var feeDa = new FeeDA();
var dbFee = new Domain.Entities.Fee
{
OrderRequestId = requestId,
Identifier = fee.Id,
Value = fee.Value,
};
feeDa.Save(dbFee);
tscope.Complete();
}
}
Run Code Online (Sandbox Code Playgroud)
Context.ExecuteFunction<..>("AddOrderRequest",...)feeDA.Save() 将实体添加到Repository和调用中 Context.SaveChanges()The transaction operation cannot be performed because there are pending requests working on …我从数据库中撤回了一个实体对象,我需要将日期更新到数据库服务器的日期/时间 - 通常你可以通过使用SQL getDate()函数设置它来实现这一点.
如何在以下场景中完成此操作:
var client = context.client.Where(c=>c.clientID == 1).FirstOrDefault();
// the value needs to be the value of the server's current date, i.e, getDate()... not DateTime.Now
client.someDate = <somevalue>;
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud) 注意:我使用的是MVC3 + Razor,EF4,CF-CTP5
如何让视图能够在客户端上动态地为每个组织添加多个Address类,并在post上强烈绑定到模型?
如果(ModelState.IsValid == false),如果输入3个地址并发布无效模型,如何重新填充数字地址及其适当的值,如何在模型中使用视图解析值?
这是我的模特:
public class Organization
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
...
}
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State …Run Code Online (Sandbox Code Playgroud) 我第一次使用实体框架,并注意到实体对象返回实体集合.
DBEntities db = new DBEntities();
db.Users; //Users is an ObjectSet<User>
User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
user.Posts; //Posts is an EntityCollection<Post>
Post post = user.Posts.Where(x => x.PostID == "123").First(); //Is this getting executed in the SQL or in memory?
Run Code Online (Sandbox Code Playgroud)
ObjectSet和EntityCollection都实现了IQueryable吗?我希望他们这样做,我知道查询是在数据源而不是在内存中执行的.
编辑:所以显然EntityCollection没有ObjectSet.这是否意味着我会更好地使用此代码?
DBEntities db = new DBEntities();
User user = db.Users.Where(x => x.Username == "test").First(); //Is this getting executed in the SQL or in memory?
Post post …Run Code Online (Sandbox Code Playgroud) 在我的SQL Server数据库中,我使用Id的自动增量字段.每当我使用SQL Server实体向该表添加新行时,我希望这些ID保持在实体框架中添加这些行的顺序 - 但由于某种原因,它是随机的.
我想我是在圈子里.
我正在使用EF4和POCO(数据库优先)和IoC开发MVC 3解决方案.我的存储库和UoW模式主要是从本文和本文中采用的.
我的解决方案由以下项目组成:
实现:
接口:
最后,IoC项目将所有内容联系在一起.
如果您在第一篇文章中注意到,作者提到从域服务中删除对ObjectSet的依赖.我假设这是为了可测试性.但问题是,它阻碍了从域服务执行复杂查询的能力,因为IObjectSet和IEnumerable(由存储库上的大多数方法返回)不存在复杂查询的方法.
这是否意味着我应该在我的存储库中进行复杂的查询?我是否应该放弃类似的方法public T Single(Expression<Func<T, bool>> where)并坚持使用类似的方法public T GetUserById(int id)?
如果不是这种情况,那么如何在我的服务层中执行此类复杂查询?
看看上面的解决方案大纲和我遇到的问题,我是朝着正确的方向前进,还是我为自己创造了问题?
提前致谢.
domain-driven-design entity-framework repository inversion-of-control entity-framework-4
我把我的edmx Metadata Artifact Processing设置为Copy to Output Directory.使用模型将csdl/msl/ssdl复制到项目的bin目录中,但是引用此项目的其他项目都没有将工件复制到bin目录中.
我有几个需要这些文件的end exes/services/etc,我不想为所有这些文件添加post build任务.有什么方法可以正确部署这些资源吗?或者我可以以某种方式使用原始edmx文件(而不是csdl/msl/ssdl)?
谢谢.
我想使用通用存储库和memcached缓存EF4结果.这一切都很有效,除了我需要一个lambda表达式的唯一键表示.
例如,对通用存储库的调用可以是:
Repository<User> userRepository = new Repository<User>();
string name = "Erik";
List<User> users_named_erik = userRepository.Find(x => x.Name == name).ToList();
Run Code Online (Sandbox Code Playgroud)
在我的通用存储库函数中,我需要从lambda表达式中创建一个唯一的键,以将其存储在memcached中.
所以我需要一个可以做到这一点的功能
string GetPredicate(Expression<Func<T,bool>> where)
{
}
Run Code Online (Sandbox Code Playgroud)
函数的结果应该是这个字符串x=> x.Name == "Erik".而不是+<>c__DisplayClass0)我从这个ToString()方法得到的东西.
提前致谢.
运行我的第一个asp.net mvc应用程序时遇到此错误我认为实体框架会自动创建以Id结尾的列名的键吗?不正确吗?
如您所见,ApplicantPositionID将是一个包含2列作为主键的表,因为它与申请人和职位相关.
在模型生成期间检测到一个或多个验证错误:
System.Data.Edm.EdmEntityType: : EntityType 'ApplicantImage' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'ApplicationPositionHistory' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicantsPositions? is based on type ?ApplicantPosition? that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicantImages? is based on type ?ApplicantImage? that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicationsPositionHistory? is based on type ?ApplicationPositionHistory? that has no keys defined.
Run Code Online (Sandbox Code Playgroud)
此行中出现错误:
public ActionResult Index()
{
return …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework entity-framework-4 entity-framework-4.1 asp.net-mvc-3
c# ×3
.net ×2
linq ×2
asp.net ×1
asp.net-mvc ×1
inheritance ×1
lambda ×1
memcached ×1
repository ×1