Tag: entity framework

Code First Entity Framework – 更改连接字符串

如何在代码第一entity framework/ MVC应用程序中更改连接字符串? 我正在尝试将其转移到实际站点,但它忽略了Web配置值,仍然引用了我的本地版数据库。 这是我的web.config的连接字符串部分: 我不确定实体字符串是否有任何相关性,因为我使用Code Firstentity framework,我认为只有在我尝试创建edmx文件时才出现(尽管我最终只删除了它)。 实体连接字符串已被注释掉,所以我不认为它已被使用。 我希望entity framework读取“WebsiteConnectionString”,但它似乎想要使用本地连接字符串,但我甚至无法看到设置的位置。 我该如何改变它?

在HasOptional()中映射外键。entity framework6中的WithOptionalDependent()关系

我在Entity Framework 6.1.3中有以下数据模型: using System.Data.Entity; public class Student { public int Id { get; set; } public virtual Contact Contact { get; set; } } public class Contact { public int Id { get; set; } public virtual Student Student { get; set; } } public class MyContext : DbContext { protected override void OnModelCreating(DbModelBuilder […]

Linq All on empty collection

我需要检查所有定义是否包含一些特定数据。 除了GroupBy返回空集合的情况外,它工作正常。 var exist = dbContext.Definitions .Where(x => propertyTypeIds.Contains(x.PropertyTypeId) && x.CountryId == countryId) .GroupBy(x => x.PropertyTypeId) .All(…some condition…); 如何重写这样所有All将在空集合上返回false? 更新:这是一个LINQ to SQL,我想在单个调用中执行它。 更新2:我认为这有效: var exist = dbContext.Definitions .Where(x => propertyTypeIds.Contains(x.PropertyTypeId) && x.CountryId == countryId) .GroupBy(x => x.PropertyTypeId) .Count(x => x .All(…some condition…)) == propertyTypeIds.Count;

如何在构建动态LINQ表达式时检测IsNull / NotNull?

我正在构建动态LINQ表达式,稍后会对其进行评估。 因此,例如,如果我想知道某些属性是否等于某个值,我会这样做: // MemberExpression property; // int? val; Expression.Equal(property, Expression.Constant(val)) 但是,我似乎无法找到一种方法来检测val是Null还是NOT Null。 有人可以向我推荐怎么做吗? 我试过这个: Expression.Equal(property, Expression.Constant(null, property.Type)); 但很明显,那是行不通的。

我们可以在entity framework中拥有没有主键的表吗?

我只是从msdn实践代码第一个新的数据库entity framework,我想知道是否可以在代码中创建一个没有主键的表第一个新的数据库EF?

entity frameworkFluent API不考虑基类属性

EF 6.1: 我们刚刚开始了一个有很多pfinheritance的项目。 选定的inheritancedb映射类型是每个层次结构的表。 问题是,在尝试使用add-migration生成迁移时,会引发以下错误: The foreign key component ‘VersionId’ is not a declared property on type ‘SER’. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. 以下是使用的类和配置类: public class Version : BaseObject { public virtual ICollection ListOfSER { get; set; } } public abstract […]

ReferentialConstraint中的依赖属性映射到存储生成的列。 专栏:’ID’

当我尝试添加Venue对象并调用SaveChanges()时,我无法弄清楚为什么会出现此错误。 具有Venue对象的模型的唯一区别是它们与City的1到1..0关系。 City city = ProcessCityCache(ev, country, db); // After this call, ‘city’ is already persisted. venue = new Venue { TicketMasterURL = ev.VenueSeoLink, Name = Capitalize(ev.VenueName), City = city }; db.Venues.AddObject(venue); db.SaveChanges(); // Exception thrown here. 任何见解将不胜感激! (在自己的选项卡/窗口中打开图像以查看完整尺寸)

Entity Framework Code First中有效的原始属性是什么?

当我尝试将列映射到我的模型类中的char数据类型时,我收到一个错误: 属性'[ColumnName]’不是'[ClassName]’类型的声明属性。 使用Ignore方法或NotMappedAttribute数据批注validation是否未从模型中显式排除该属性。 确保它是有效的原始属性。 EF Code First有效的基本类型是什么?

通过EF / Linq投射到KeyValuePair

我正在尝试从EF / Linq查询中加载KeyValuePairs列表,如下所示: return (from o in context.myTable select new KeyValuePair(o.columnA, o.columnB)).ToList(); 我的问题是,这会导致错误 “LINQ to Entities中仅支持无参数构造函数和初始值设定项。” 这有简单的方法吗? 我知道我可以为此创建一个自定义类而不是使用KeyValuePair,但这似乎重新发明了轮子。

运算符’==’不能应用于linq到实体的’System.Guid’和’string’类型的操作数

我收到此错误’运算符’==’不能应用于linq中类型’System.Guid’和’string”的操作数到代码下面的entityframework。 在下面的代码中,CustomerId是Guid,customerProfileId是字符串。 var accountQuery = from C in CustomerModel.CustomerProfile where C.CustomerId == customerProfileId // Error here select C;