Tag: entity framework

为什么Linq包含生成此SQL?

背景:我正在开发一个系统来清理内部客户列表,并找出我们拥有该公司其他人的电子邮件地址的联系人的电子邮件地址。 为了做到这一点,我有(简化)3个表: 联系人 : ID CompanyId Email Domain 电子邮件域名 : ID Domain EmailFormat EmailFormatConfirmed 我有一个手动例程说,给定公司,我找到下一个联系人,我们有他们的域名但不是他们的电子邮件地址: int companyId = 53; var emails = Contacts.Where(p => p.companyId == companyId && p.Email == null && !string.IsNullOrEmpty(p.Domain)).Select(p => p.Domain); var domain = EmailDomains.FirstOrDefault( d => !d.EmailFormatConfirmed && !string.IsNullOrEmpty(d.Domain) && emails.Contains(d.Domain)); 此查询运行速度非常慢,并且检出生成的Sql: — Region Parameters DECLARE @p__linq__0 Int = 53 […]

以编程方式获取entity framework中的Property的’Nullable’状态

我需要为EF中的字段获取Nullable属性。 一些魔法代码需要在Nullable = True的属性上完成,我找不到一个有效的解决方案来获取属性。 foreach (PropertyInfo property in (PropertyInfo[])type.GetProperties()) { var getPropertyType = property.GetMethod.ReturnTypeCustomAttributes.ToString(); var getValue = property.GetValue(model); bool isNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable); // isNullable always returns false !!!! and I need it to return true if the field is allowed to be null if ((getValue == null) && (!isNullable)) { } […]

如何指定从实体X到同一目标实体Y的两个导航属性?

考虑一下我有一个Instructor类: public class Instructor { public InstructorTypesEnum Type { get; set; } public virtual ICollection Courses { get; set; } public virtual ICollection CoInstructingCourses { get; set; } } 这里, InstructorTypesEnum有两个值:讲师和辅导员。 所以两个导航属性Courses和CoInstructingCourses应该返回那些不同的课程。 当然,我也很难在Course类中指定相应的属性 public Guid InstructorId { get; set; } public virtual Instructor Instructor { get; set; } public virtual ICollection Coinstructors { get; set; […]

entity framework4.0奇怪,保存数据

我遇到了下一个问题。 我有一个带模型的数据仓库 public class GameResult { public int GameResultId { get; set; } public virtual Competition Competition { get; set; } public virtual CustomDate DateGame { get; set; } public virtual Contender ContenderFirst { get; set; } public virtual Contender ContenderSecond { get; set; } public virtual Location Location { get; set; } } public […]

在购物车的会话状态中存储数据

试图把我存储的内容添加到购物车中,然后将其转移到另一个页面,以便让GridView显示我添加到购物车会话中的所有商品。 将其存储为对象会话。 -AddToCart在A Session中获取行详细信息和存储,然后获取该会话对象并将其显示在另一页上的Grid视图中。 从中获取此代码: protected void GridViewDisplay_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == “AddToCart”) { object[] values; DataTable orderTable; // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridViewDisplay.Rows[index]; values = […]

在Entity Framework中使用ModelConfiguration会产生错误“不是有效的属性表达式”

在我的C#项目中,当EF尝试创建我的数据库时出现错误。当我调用时发生错误 Database.SetInitializer(new CreateDatabaseIfNotExists()); 错误消息是 The expression ‘x => x.Dependancies’ is not a valid property expression. The expression should represent a property: C#: ‘t => t.MyProperty’ VB.Net: ‘Function(t) t.MyProperty’. 我的域类如下 [Table(“LoggedEntity”)] public class LoggedEntity { public int Id { get; set; } } [Table(“TemplateTaskDependancy”)] public class TemplateTaskDependancy : LoggedEntity { [Column] public int NeededTaskId { get; […]

使用安全向下转换的类设计选项

在经历了向下倾斜(见[我的原帖] )并制作[深拷贝]之后 ,我发现[这个同名文章]在C ++中提出了如何处理这个问题的建议。 我非常兴奋,我在C#中实现了如下: public partial class User { virtual public Employer GetEmployer() { return null; } … } public partial class Employer { public override Employer GetEmployer() { return this; } … } 然后我像这样使用: User u = GetUser(); Employer e = u.GetEmployer(); 但是(我想,毫不奇怪),从不调用覆盖并返回null。 我试图解决的问题是我收集的将是一个非常常见的用例:我得到一些我需要存储的数据,但它不完整。 后来我获得了更多数据,并用它来改进(低估)我对世界的理解。 在这种特殊情况下,我从使用我网站的人那里得到一个电子邮件地址,所以我知道他们是User ,但总的来说,我不知道其他任何关于他们的信息。 后来(当他们填写表格时),我知道他们实际上是Employer ,所以我需要低估我的User 。 这里的正确方法是什么?

在c#中映射两个类

我有两节课 public class foo1 { public int id; public string image_link; public string sale_price; } 和 public class foo2 { public int Id; public string ImageLink; public string SalePrice } 属性值仅因下划线和案例而异。 我需要映射这两个类。 现在我正在尝试这样的事情及其工作: //var b = object of foo2 var a = new foo1{ a.id = b.Id, a.image_link = b.ImageLink, a.sale_price = b.SalePrice } 我听说过AutoMapper,但我还不清楚我将如何使用它,或者忽略其中的案例或下划线的选项。 […]

如何使用Exact数组值而不是包含

我需要使用Equals方法或类似的东西,而不是使用Contains方法,因为我想在数据库中搜索selectedDeviceTypeIDs数组中的确切值而不是任何一个。 IEnumerable selectedDeviceTypeIDs = DeviceTypeIDs .Split(‘,’) .Select( Guid.Parse ) .AsEnumerable(); query = query .Where( j => j.HospitalDepartments.Any( jj => jj.Units.Any( m => m.Devices.Any( w => selectedDeviceTypeIDs.Contains( w.DeviceTypeID ) ) ) ) ); 这是我的完整代码 public HttpResponseMessage GetAvailableHospitalsByAjax(System.Guid? DirectorateOfHealthID = null, System.Guid? UnitTypeID = null, string DeviceTypeIDs = null) { Context db = new Context(); var query […]

如何让EntityFramework检查2个参数?

我有3个相关对象(为简洁起见省略了非相关属性): public class Product { public int ID { get; set; } public virtual ProductPrice Price { get; set; } } public class ProductPrice { public int ID { get; set; } public int ProductID { get; set; } public int VerticalID { get; set; } public decimal Value { get; set; } public virtual […]