C#中的EF 4.
我有我的查询,目前我能够按当前日期过滤结果(只是日期不考虑TIME).
我需要将结果从过去两天过滤到当前日期(不知道怎么做).
我在查询中尝试currenteDate - 2但没有成功.
你能举个例子吗?谢谢你的时间.
DateTime currenteDate = DateTime.UtcNow.Date;
var contents = from cnt in context.CmsContents
where cnt.IsPublished == true & cnt.IsDeleted == false & cnt.Date == currenteDate
orderby cnt.ContentId descending
select new { cnt.ContentId, cnt.Title, cnt.TitleUrl, cnt.Date, cnt.TypeContent };
Run Code Online (Sandbox Code Playgroud) 我将开发一个相当大的winform项目.我打算使用Entity Framework作为ORM工具.现在我正在寻找一个架构(MVC/MVP/MVVM等......)来实现所有这些.首先,对于Windows窗体的n层架构几乎没有选择,我得到的大部分都是在EF进入市场之前编写的.我从codeplex获得了一个名为Rocket Framework的框架(http://rocketframework.codeplex.com)
我环顾了一下,但我怀疑它会满足各种各样的要求.如果有人已经发现了车轮,请指导我.此外,如果EF4之前的现有架构可以容纳它,我也可以尝试一下.好主意!
我想要做的基本上就是当你做类似的事情时NHibernate所做的事情:
var instance = session.Get<Customer>(id, LockMode.Upgrade);
Run Code Online (Sandbox Code Playgroud)
我需要锁定数据库中的实体行.为什么我需要那个?想象一下,工作流实例可以同时由多个角色(人或进程)更新.
我的约束不允许乐观锁定解决方案.
框架:Silverlight 4 + Entity Framework 4(SL业务应用程序:解决方案中的SL项目和.WEB项目)
我的数据层中有两个类(未映射到DB,手动创建 - 我需要它们用于特定视图)
[EnableClientAccess]
public class CityInfoFull
{
[Key]
public int Id { get; set; }
public String Country{ get; set; }
public String Region { get; set; }
public String City { get; set; }
public int Population { get; set; }
public DateTime Founded { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
RIA在Geography.Web.g.cs中生成适当的代理类
在同一个命名空间中,我有另一个类:
[EnableClientAccess]
public class Person
{
[Key]
public int Id { get; set; }
public String FullName { get; set; }
public …Run Code Online (Sandbox Code Playgroud) c# code-generation entity-framework-4 silverlight-4.0 wcf-ria-services

我是EF的新手,并且上面有以下实体模型,其中包括资产和国家.资产可以属于许多国家/地区,因此与国家/地区具有多对多关系(数据库中具有连接表,两个字段都作为主键).
我希望能够做到以下几点:
首先,当我从模型中检索资产(或资产)时,我希望获得与其关联的各个国家/地区.然后,我希望能够将国家/地区列表绑定到IEnumerable.以这种方式检索国家为我提供了一个国家对象的EntityCollection,它具有ToList()的扩展方法.因此不确定如果我正沿着正确的大道走下去.这是我的GetAll方法:
public IEnumerable<Asset> GetAll()
{
using (var context = CreateAssetContext())
{
var assetEntities = context.Assets.Include("Countries").ToList();
return AssetMapper.FromEntityObjects(assetEntities);
}
}
Run Code Online (Sandbox Code Playgroud)
其次,我希望能够选择AssetId ==某个值的国家/地区列表.
最后,我希望能够更新给定资产的国家/地区列表.
非常感谢.
对于这个Demo,我创建了一个虚假的Database +存储库,如下所示
public interface IDemoRepository
{
string[] GetUsers();
}
public class DemoRepository : IDemoRepository, IDisposable
{
public string[] GetUsers()
{
string[] Users = { "Robert","Linda","Jack"};
return Users;
}
public void Dispose()
{
//do nothing
throw new Exception("Disposed is called");
}
}
Run Code Online (Sandbox Code Playgroud)
public class TestController:Controller
{
protected IDemoRepository _repository;
public BaseController(IDemoRepository repository)
{
_repository = repository;
}
public ActionResult()
{
var users = _repository.GetUsers();
Return View(users);
}
}
Run Code Online (Sandbox Code Playgroud)
我从NUGet安装了ninject并在下面添加了用于解析存储库的代码
kernel.Bind<IDemoRepository>().To<DemoRepository>()
Run Code Online (Sandbox Code Playgroud)
Ninject没有调用DemoRepository.Dispose,我添加了一个断点,即使我当前的代码抛出错误但是Ninject没有调用DemoRepository.Dispose.
任何人都能建议我如何处置这个物体.
我正在尝试加载SingleOrDefault实体的相关实体,但是我收到以下异常:
IEnumerable类型的导航属性不是ICollection类型的单个实现
我尝试过这几种方法,并最终针对上下文针对每个查询获得相同的错误(我在评论中包含了其他方式).
using(var context = CustomObjectContextCreator.Create())
{
return context.Job.Include("Surveys").Include("SiteInfoes")
.Where(r => r.Jobid == jobId).SingleOrDefault();
//context.ContextOptions.LazyLoadingEnabled = false;
//var Job = context.Job.Where(r => r.Jobid == jobId).SingleOrDefault();
//context.LoadProperty(Job, "Surveys");
//context.LoadProperty(Job, "SiteInfoes");
//var Job = (from j in context.Job
// .Include("Surveys")
// .Include("SiteInfoes")
// select j).SingleOrDefault();
//var Job = context.Job.Where(r => r.Jobid == jobId).SingleOrDefault();
//var surveys = context.Surveys.Where(s => s.JobID == jobId);
//var wellInfoes = context.SiteInfoes.Where(w => w.Jobid == jobId);
//Job.Surveys = surveys.ToList();
//Job.SiteInfoes = wellInfoes.ToList();
//return Job;
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的POCO对象:
public class …Run Code Online (Sandbox Code Playgroud) 我有3个表需要与实体框架映射,我不知道正确的方法来解决这个问题.这是我的3个实体:
public class User
{
[Key]
public int user_id {get; set;}
public string user_name {get; set;}
public virtual List<Role> roles {get; set;}
}
public class Role
{
[Key]
public int role_id {get; set;}
public string role_name {get; set;}
}
public class User_Role
{
[Key]
public int user_role_id {get; set;}
public int user_id {get; set;}
public int role_id {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
请注意,User_Role实体只表示一个查找表,用于将多个角色链接到单个用户.
使用SQL我会做类似的事情:
SELECT User.user_name, Role.role_name FROM User INNER JOIN User_Role ON User_Role.user_id = User.user_id INNER JOIN Role ON Role.role_id = …Run Code Online (Sandbox Code Playgroud) mapping lookup entity-framework inner-join entity-framework-4
我试图使用EF 4.1 Code First来建模具有单个角色的用户的简单关系.当我尝试将具有新角色的现有用户保存到不同的上下文(使用不同的上下文来模拟客户端 - 服务器往返)时,我得到以下异常:
System.Data.Entity.Infrastructure.DbUpdateException:保存不公开其关系的外键属性的实体时发生错误.EntityEntries属性将返回null,因为无法将单个实体标识为异常的来源.通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常.有关详细信息,请参阅InnerException.---> System.Data.UpdateException:来自'User_CurrentRole'AssocSet的关系处于'已添加'状态.给定多重约束,相应的'User_CurrentRole_Source'也必须处于'已添加'状态.
我期望创建一个新角色并与现有用户相关联.
我做错了什么,这有可能首先在EF 4.1代码中实现吗?错误消息似乎表明它需要用户和角色都处于添加状态,但我正在修改一个exising用户,那怎么可能呢?
注意事项:我想避免修改实体的结构(例如,通过引入实体上可见的外键属性),并且在数据库中我希望用户有一个指向Role的外键(不是其他方式).我也不准备转移到自我跟踪实体(除非没有别的办法).
以下是实体:
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public Role CurrentRole { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的映射:
public class UserRolesContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; …Run Code Online (Sandbox Code Playgroud) 我有简单的查询,将两个表中的数据加载到GUI中.我正在将加载的数据保存到广泛可用的对象中Clients currentlySelectedClient.
using (var context = new EntityBazaCRM())
{
currentlySelectedClient = context.Kliencis.Include("Podmioty").FirstOrDefault(d => d.KlienciID == klientId);
if (currentlySelectedClient != null)
{
textImie.Text = currentlySelectedClient.Podmioty.PodmiotOsobaImie;
textNazwisko.Text = currentlySelectedClient.Podmioty.PodmiotOsobaNazwisko;
}
else
{
textNazwa.Text = currentlySelectedClient.Podmioty.PodmiotFirmaNazwa;
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在如果我想:
1)保存用户所做的更改我该怎么做?我是否必须在数据库方面准备一些东西?如何处理修改多个表(某些数据在这里,有些数据在那里)?我现在的代码好像写了.KlienciHaslo就好了,但它根本不影响Podmioty.我尝试了不同的组合,但没有运气.
2)将新客户端添加到数据库(并将信息保存到相关表)?
currentClient.Podmioty.PodmiotOsobaImie = textImie.Text; // not saved
currentClient.Podmioty.PodmiotOsobaNazwisko = textNazwisko.Text; // not saved
currentClient.KlienciHaslo = "TEST111"; // saved
using (var context = new EntityBazaCRM())
{
var objectInDB = context.Kliencis.SingleOrDefault(t => t.KlienciID == currentClient.KlienciID);
if (objectInDB != null)
{
// context.ObjectStateManager.ChangeObjectState(currentClient.Podmioty, EntityState.Modified); …Run Code Online (Sandbox Code Playgroud) c# ×6
winforms ×2
idisposable ×1
inner-join ×1
linq ×1
lookup ×1
mapping ×1
n-layer ×1
ninject ×1
sql-server ×1