我正在使用实体框架4并有一个问题:
我的数据库中有一个密码列,我想使用自定义SQL进行管理.所以我不希望模型对它有任何了解.
我尝试在Mapping Details窗口中删除该属性,但后来出现了编译错误:
错误3023:从第1660行开始映射片段时出现问题:表中的User User.Password必须映射User:它没有默认值且不可为空.
因此,我在数据库中使列可以为空并更新了模型.现在我收到这个错误:
错误3004:从第1660行开始映射片段时出现问题:没有为Set Users中的属性User.Password,User.Salt指定映射.具有密钥(PK)的实体在以下情况下不会往返:实体类型为[UserDirectoryModel.User]
有什么想法吗?
谢谢,尼克
假设我有2个表:Message和SuperMessage
和3个实体:消息(基础(不是抽象)),注释(继承自Message)和SuperMessage(继承自Message)
消息具有不可为空的MessageType字段,该字段用作鉴别器.
问题是我无法在SuperMessage的Mapping详细信息部分的MessageType上指定条件,因为它无法看到MessageType字段,我无法忽略它.
我怎样才能让这些与彼此并肩作战呢?
更新构建错误:
错误3014:映射片段中的问题:外键"外键约束"FK_SuperMessage_inherits_Message'从表SuperMessage(ID)到表Message(MessageId):'未在模型中强制执行.需要创建关联或继承关系以强制执行此约束.
我正在使用Entity Framework 4构建一个ASP.Net MVC 3应用程序.当执行下面两段代码时,两个变量(query1和query2)的返回类型都是
System.Data.Objects.ObjectQuery<Asset.Model.Equipment>
Run Code Online (Sandbox Code Playgroud)
Query1使用ObjectContext的直接实例,但是,Query2使用存储库模式,即它在EquipmentService中调用GetEquipment,后者又在Equipment Repository中调用相同的命名方法.Service和Repository中的两个方法都返回
IQueryable<Equipment>
Run Code Online (Sandbox Code Playgroud)
如何,这是我的问题,为什么query2只有在我包含时才会起作用
using System.Linq.Dynamic;
Run Code Online (Sandbox Code Playgroud)
在我的控制器的顶部
using (AssetEntities context = new AssetEntities())
{
var query1 = context.Equipments
.OrderBy("it." + sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
}
var query2 = equipService.GetEquipment()
.OrderBy(sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
Run Code Online (Sandbox Code Playgroud)
如果我从我的控制器omitt System.Linq.Dynamic,我在Query2中得到一个错误
.OrderBy(sidx + " " + sord)
Run Code Online (Sandbox Code Playgroud)
哪个州
The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么query1可以工作而不必使用System.Linq.Dynamic,但是query2需要它来执行? …
linq-to-entities jqgrid dynamic-linq entity-framework-4 asp.net-mvc-3
我需要绑定GridView,我正在使用此代码:
ProductDBEntities db = new ProductPDBEntities();
var pro = from u in db.Products where u.PID == 1 select u;
if (pro != null)
{
GridView1.DataSource = pro;
GridView1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
并得到这个错误.
System.InvalidOperationException:Sequence包含多个元素
有人可以告诉我,我做错了什么?
我开始使用MVC 3并使用MvcScaffolding来构建这些模型:
namespace Conference.Models
{
/*
* Speaker can have many session
* And session can have many speakers
*/
public class Speaker
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
public class Session
{
public Guid Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; …Run Code Online (Sandbox Code Playgroud) 在我的数据库中,我有以下表格:
Person-InterestTag和Post-InterestTag之间存在许多关系
我需要在EF 4.1中执行linq查询以撤回包含至少一个与至少一个与给定用户相关的兴趣标签匹配的兴趣标签的任何帖子.
例
一个人有以下兴趣:
我需要退回任何与汽车,运动或健身相关的帖子.
在性能方面编写此查询的最有效方法是什么?
编辑
根据下面给出的答案遇到错误...
这编译得很好但在运行时抛出错误:
var matchingPosts = posts.Where(post => post.Topics.Any(postTopic => person.Interests.Contains(postTopic)));
Run Code Online (Sandbox Code Playgroud)
错误是:
Unable to create a constant value of type 'System.Collections.Generic.ICollection`1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这一问题?
编辑2
所以我的课程结构如下:
public class Person
{
public int PersonID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
//other properties of types string, int, DateTime, etc.
public …Run Code Online (Sandbox Code Playgroud) 使用MVC3更新对象
我有一个可以修改的模型,请看下面的示例:
[HttpPost]
public ActionResult Edit(Company c)
{
if (ModelState.IsValid)
{
db.Entry(c).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
Run Code Online (Sandbox Code Playgroud)
该模型具有未在视图中显示的其他字段,并且无法由用户修改,但是当我单击提交按钮时,未在视图中显示的字段设置为null.
我可以以某种方式让EF知道不修改某些字段吗?谢谢.
假设我有以下实体
public abstract class Animal
{
public int Id {get;set;}
}
public class Cat : Animal
{
}
public class Dog : Animal
{
}
Run Code Online (Sandbox Code Playgroud)
是否可以在不创建实例的情况下确定实体的类型.
var id = 1;
var type = context.Animals.GetTypeOfAnimal(id)
public static Type GetTypeOfAnimal(this ObjectSet<Animal> source, int id)
{
// What shall I do here, I dont want to fetch the instance at this point...
var animal = source.First(a => a.Id == id);
return animal.GetType();
}
Run Code Online (Sandbox Code Playgroud)
我考虑使用以下方法的一个解决方案......
public static Type GetTypeOfAnimal(this ObjectSet<Animal> source, int id)
{ …Run Code Online (Sandbox Code Playgroud) 我想保存一些具有双向关系的实体(两端的导航属性).这是通过2次调用来完成的context.SaveChanges().
[关于我的模型,映射以及我如何到达那里的完整细节都在折叠之后.]
public void Save(){
var t = new Transfer();
var ti1 = new TransferItem();
var ti2 = new TransferItem();
//deal with the types with nullable FKs first
t.TransferIncomeItem = ti1;
t.TransferExpenseItem = ti2;
context.Transfers.Add(t);
context.Operations.Add(ti1);
context.Operations.Add(ti2);
//save, so all objects get assigned their Ids
context.SaveChanges();
//set up the "optional" half of the relationship
ti1.Transfer = t;
ti2.Transfer = t;
context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,但如果在两次调用之间发生雷击,确保数据库不一致怎么样SaveChanges()?
输入TransactionScope...
public void Save(){
using (var tt = new TransactionScope())
{ …Run Code Online (Sandbox Code Playgroud) 我第一次使用EntityFramework也许这个问题很简单......我已经使用了代码优先方法..我有一个类人员,看起来像这样:
public class Personnel
{
public string Id { set; get; }
public int Code { set; get; }
public string Name { set; get; }
public int Type { set; get; }
public JobTitle Title { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
和JobTitle类:
public class JobTitle
{
public string Id { set; get; }
public int Number { set; get; }
public string Title { set; get; }
public List<Personnel> Personnels { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
其中Personnel Class中的最后一个属性当然是人员表中的外键.我的问题是当我想使用lambda表达式从DB检索所有人员(或人员)时...外键对象为空... lambda表达式如下:
Context.ContextInstance.Personnels.ToList();
Run Code Online (Sandbox Code Playgroud)
如果我将表达式更改为此,则外键对象不再为null. …
c# ×4
asp.net ×2
asp.net-mvc ×1
c#-4.0 ×1
dynamic-linq ×1
edmx ×1
jqgrid ×1
linq ×1
scaffolding ×1
transactions ×1