Tag: entity framework

EF GroupBy:不包含’BriefTitle’的定义,也没有扩展方法’BriefTitle’接受类型的第一个参数

这个查询有什么问题? var model = SessionObjectsMSurvey.ContractList .Where(y => y.ContractTitle.ToUpper().Contains(upper)) .GroupBy(g => new { g.BriefTitle, g.ContractId }) .Select( x => new { label = x.BriefTitle, id = x.ContractId.ToString() }).Take(20); SessionObjectsMSurvey.ContractList是一个IEnumerable集合。 这不会编译,我得到; 错误13’System.Linq.IGrouping’不包含’BriefTitle’的定义,并且没有扩展方法’BriefTitle’接受类型’System.Linq.IGrouping’的第一个参数可以找到(你是否缺少using指令或者assembly参考?)

我应该为创建和更新设置不同的DTO吗? (CRUD)

我正在使用Person实体上的常规CRUD操作设计Web API。 问题是我不知道如何设计DTO。 该实体如下: public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } 我已经确定DTO应该有相同的成员: public class PersonDto { public int Id { get; set; } public string Name { get; set; } public int Age{ get; set; } […]

比较2个列表之间的值并减去常见元素的值 – C#

我需要准备一张图表,其中我需要显示3行。 一个用于显示一周的新问题,第二个用于一周的已结束问题,第三个用于一周的总公开问题。 出于这个原因,我准备了一个查询,并且能够成功创建2个单独的列表 – 一个列表维护每周新发布的计数,第二个列表维护每周的已关闭问题计数。 以下是第一个列表的示例数据(一个维护新问题的列表): [0]: { Week = {6/14/2015 12:00:00 AM}, Count = 1 } [1]: { Week = {3/5/2017 12:00:00 AM}, Count = 1 } [2]: { Week = {5/21/2017 12:00:00 AM}, Count = 4 } [3]: { Week = {6/4/2017 12:00:00 AM}, Count = 7 } [4]: { Week = {6/11/2017 […]

如何轻松地将DbDataReader对象转换为有用的东西?

我使用Entity Framework EntityConnection手动调用存储过程,如下所示: DbConnection storeConnection = entityConnection.StoreConnection; DbCommand command = storeConnection.CreateCommand(); command.CommandText = “sp_GetMyComplexData”; command.CommandType = CommandType.StoredProcedure; DbDataReader reader = command.ExecuteReader(); 这样做的原因是entity framework不容易支持不直接映射到表或视图的实体。 我发现这篇文章( http://dotnet.dzone.com/news/mapping-stored-procedure ),但它需要编辑XML,我想避免。 在我的情况下,我只是运行一个使用LEFT JOIN连接两个表的查询。 之所以这样,是因为我试图将结果绑定到Telerik RadGrid,它不支持分层数据导出,但似乎支持分组数据导出。 问题是,如何将DbDataReader对象转换为可以绑定到RadGrid的有用的东西? 我一直在DAL方法中传递单个对象或IQueryable对象。 有任何想法吗? 提前致谢? 编辑: 我只需要数据。 之后我不需要进行任何插入,更新和删除操作。 它是一个只读视图。

.NET Core 2.0模块化dependency injection

我正在尝试构建一个包含Entity Framework及其数据库提供程序等核心和扩展包的库。 我想要做的是当我使用dependency injection注册该库时,我想将特定实现作为参数。 想想EF。 为了在EF上使用sql提供程序,我们需要将其作为选项参数传递给SQL提供程序,如下所示。 services.AddDbContext(options => { options.UseSqlServer(Configuration[“ConnectionString”]); }); 我想建立类似的结构。 让我们说我的框架将提供电影制片人。 它将包含用于框架相关类的producer.core包和两个名为Producer.Extensions.Hollywood和Producer.Extensions.Bollywood扩展包。 如果我想使用Hollywood提供商,我需要安装核心包和Hollywood扩展包。 注册时应该看起来像 services.AddFilmProducer(options => { options.UseHollywoodProducer(); }); 我找不到一个能给我指路的关键词。 我试图阅读entity framework的源代码,但对我的情况来说太复杂了。 有没有人可以指点我的方向? 提前致谢。

entity framework包含没有嵌套属性

我有两个名为DataTag和TagSource的相关实体,如下所示: public class DataTag : BaseModel { [Column(“DataTagId”)] public override Guid ID { get; set; } public string Tag { get; set; } public Guid TagSourceId { get; set; } public TagSource TagSource { get; set; } } public class TagSource : BaseModel { [Column(“TagSourceId”)] public override Guid ID { get; set; } public string […]

使用Base类将字符串传递给DbContext的构造函数不会设置该值

我决定不再需要对我的DbContext类进行更改 以前我的代码看起来像这样: public class RPMContext : DbContext { public RPMContext() : base(ConnectionString()) { } private static string ConnectionString(string connection) { conn = ConfigurationManager.ConnectionStrings[“LH_RPMContext”].ConnectionString; return conn; } } 在“Works”之上,但我需要能够在一个字符串中进行PASS,我可以将静态方法ConnectionString设置为OTHER指定的连接字符串 因此, 我的新代码开始沿着不使用基础的路径 public class RPMContext : DbContext { public RPMContext(string environment) { ConnectionString(environment); } private static string ConnectionString(string connection) { string conn; if (connection == “LH”) { […]

在entity framework6.1中模拟DbContext

我发现了一些例子(显然)显示了用EF 6模拟DbContext的明显工作示例,然而,它们似乎都不适合我,我不完全确定原因。 这是我设置模拟的unit testing代码; var mockData = new List { new User { Email = “my@email.com”, Id = 1 } }.AsQueryable(); var mockSet = new Mock<DbSet>(); mockSet.As<IQueryable>().Setup(m => m.Provider).Returns(mockData.Provider); mockSet.As<IQueryable>().Setup(m => m.Expression).Returns(mockData.Expression); mockSet.As<IQueryable>().Setup(m => m.ElementType).Returns(mockData.ElementType); mockSet.As<IQueryable>().Setup(m => m.GetEnumerator()).Returns(mockData.GetEnumerator()); var mockContext = new Mock(); mockContext.Setup(c => c.Users).Returns(mockSet.Object); 然后调用我正在测试的服务; var service = new UsersService(mockContext.Object); var user = […]

实体类型不是当前上下文的模型的一部分

大段引用 我现在已经解决了这个问题几天了。 我正在尝试使用Visual Studio 2010 MVC2 EF 6.0创建数据库连接。 我可以使用服务器资源管理器连接到数据库。 这是我到目前为止所做的: 创建了一个Model:ModelEntities.edmx(连接到SQL Server DB) 2.为我正在尝试访问的表创建了一个Model:Table.cs(包含所有公共成员) public class Quickfix { public int FIX_ID { get; set; } public string NAME { get; set; } public string TYPE { get; set; } public string DESCRIPTION { get; set; } } 创建了一个DAL文件夹并将其上下文添加到其中:(ModelEntitesContext.cs) 使用ServiceDesk_Solution.Models; namespace ServiceDesk_Solution.DAL { public class ModelEntitiesContext […]

为什么LINQ to Entities无法识别方法’System.String ToString()?

在MVC3 Web应用程序中获取错误。 LINQ to Entities does not recognize the method ‘System.String ToString()’ method, and this method cannot be translated into a store expression. 当我尝试从查询中使用EF获取值时: public class DataRepository { public mydataEntities1 dbContext = new mydataEntities1(); public List GetPricingSecurityID() { var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing select new SelectListItem { Text = m.PricingSecurityID.ToString(), Value = m.PricingSecurityID.ToString() […]