Tag: entity framework

无法确定类型之间关联的主要结尾

我收到这个错误: 无法确定CustomerDetail和Customer类型之间关联的主要结束。 这是我的Customer和CustomerDetail模型 [Table(“CUSTOMER”)] public class Customer { [Required] [Column(“CUSTOMER_ID”)] public int Id {get; set;} [Column(“FIRST_NAME”)] public string FirstName {get; set;} // other fields public virtual CustomerDetail customerDetail {get; set;} } [Table(“CUSTOMER_DETAIL”)] public class CustomerDetail { [Required] [Column(“CUSTOMER_DETAIL_ID”)] public int Id {get; set;} // other fields public virtual Customer Customer {get; set;} } Customer与CustomerDetail关系为1:1。

连接到多租户应用程序的数据库?

我正在创建一个Multi-Tenant asp.net应用程序。 我在单独的MSSQL数据库或模式之间做出决定。 但是,我无法找到有关如何动态更改数据库(理想情况下)或用户帐户的任何信息,具体取决于用户应连接到哪个帐户。 我很可能会有一个基表来定义用户应该连接到哪个数据库。 使用Linq to SQL执行此操作非常简单,但我并不是在任何地方都使用Linq,因为这些表非常动态,而且架构很容易经常更改。 这样做的最佳方法是什么? 我很高兴看到使用Schemas,但我不希望它变得非常混乱,但我还需要做一个类似的方法,但不知何故以同样的方式冒充该用户来拾取默认模式。 我知道你可以动态更改web.config连接字符串,但我已经尝试过,它实际上更改了文件内容,这也刷新了应用程序池,并导致了很多其他问题。 谢谢

包含属性但排除该属性的属性之一

假设我的一个控制器中有这样的方法: [Route(“api/Products”)] public IQueryable GetProducts() { return db.Products .Include(p => p.Category); } 使用这个我可以从数据库中获取产品并包含其Category属性。 在我的CategoryControllerI中有这个方法: [Route(“api/Categories”)] public IQueryable GetCategories() { return db.Categories .Include(c => c.Parent) .Include(c => c.Products) .Include(c => c.SubCategories); } 当我向CategoryController发送GET请求时,它按预期工作,我得到类别,其父级,产品和子类别。 但是当我向ProductController发送GET请求时,我不想将所有产品都包含在所请求产品的类别中,我只需要有关该类别的基本信息。 那么,我如何让GetProducts()返回数据库中的产品,包括每个产品的Category属性,但不包括该类别的Products列表属性,仍然保留其他属性,如id,title等? 谢谢。

EntityFramework – 实体代理错误

我正在使用Entityframework进行系统工作,现在已经超过12个单元,而且项目进展顺利,直到昨天,我现在遇到了一个奇怪的错误,我不知道它为什么会发生。 我没有做任何与以前做过的事情不同的事情,但是一旦我加载了有问题的实体并尝试访问任何子实体,我就会收到以下错误: The entity wrapper stored in the proxy does not reference the same proxy 任何人都可以了解这实际意味着什么以及会导致什么? 显示我的代码并没有真正帮助。 这是代码的简化版本: var quote = new QuoteHelper().GetById(orderId); var updatedQuotes = new Provider().GetExportQuotes(quote.DeparturePoint.Id,quote.DestinationPoint); 访问DeparturePoint和DestinationPoint时出错,但Quote正确加载,并且所有属性都已加载。 实体Quote看起来有点像这样: public class Quote : BaseQuote, ICloneable { public Guid DeparturePointId { get; set; } public virtual LocationPoint DeparturePoint{ get; set; } public Guid DestinationPointId { […]

LINQ to Entities查询中可重用的谓词表达式

在我们的应用程序中,许多不同查询中出现的一组标准已经慢慢变得更加复杂。 为了避免重复此代码,我想将这些条件拆分为一个方法,该方法将条件作为表达式返回,然后可以在必要时应用它: public Expression<Func> GetComplexPredicate() { // complex predicate is returned as an Expression: return c => … } 重复使用: var result = repository.Invoice.Where(GetComplexPredicate()) 但是,下面的语句不会编译,因为c.Invoice只是一个ICollection 。 var result = repository.Customer .Where(c => c.Country == “US” && c.Invoice.Any(GetComplexPredicate())) 是否可以使用这样的表达式?

删除多个到多个entity framework

Artist和ArtistType之间存在多对多的关系。 我可以轻松添加艺术家ArtistType如下所示 foreach (var artistType in this._db.ArtistTypes .Where(artistType => vm.SelectedIds.Contains(artistType.ArtistTypeID))) { artist.ArtistTypes.Add(artistType); } _db.ArtistDetails.Add(artist); _db.SaveChanges(); 这将使用正确的映射更新多对多关联表。 但是,当我尝试从表中删除任何项目时,我没有得到任何错误,但它没有从表中删除它? foreach (var artistType in this._db.ArtistTypes .Where(at => vm.SelectedIds.Contains(at.ArtistTypeID))) { artistDetail.ArtistTypes.Remove(artistType); } this._db.Entry(artistDetail).State = EntityState.Modified; this._db.SaveChanges(); 我错过了什么?

存储过程返回int而不是结果集

我有一个包含动态选择的存储过程。 像这样的东西: ALTER PROCEDURE [dbo].[usp_GetTestRecords] –@p1 int = 0, –@p2 int = 0 @groupId nvarchar(10) = 0 AS BEGIN SET NOCOUNT ON; DECLARE @query NVARCHAR(max) SET @query = ‘SELECT * FROM CUSTOMERS WHERE Id = ‘ + @groupId /* This actually contains a dynamic pivot select statement */ EXECUTE(@query); END 在SSMS中,存储过程运行正常并显示结果集。 在使用Entity Framework的C#中,它显示返回一个int而不是IEnumerable ? private […]

使用Entity Framework 4.1 Fluent API在非主键字段上创建关联

我们使用EF 4.1和流畅的API从遗留数据库中获取数据(我们不允许更改)。 我们在创建两个表之间的关系时遇到问题,其中相关列不是主键和外键。 使用下面的类,我们如何配置Report和RunStat之间的一对多关系,以便Report.RunStats将返回ReportCode字段相等的所有RunStat实体? public class Report { [Key] public int ReportKey { get; set; } public string Name { get; set; } public int ReportCode { get; set; } // Can we associate on this field? public virtual ICollection RunStats { get; set; } } public class RunStat { [Key] public int RunStatKey { […]

LINQ查询将字符串转换为datetime

我想将字符串值转换为日期时间 类 public class demoDate { public DateTime DueDate; public int OrderReportID; public DateTime _DueDate { get { return DueDate; } set { DueDate = value; } } public int _OrderReportID { get { return OrderReportID; } set { OrderReportID = value;} } } 询问 var DateQuery = (from o in db.Order_Reports select new demoDate […]

Linq加入COUNT

我有2张桌子,论坛和post。 我想要使​​用新的额外字段检索所有论坛字段:计算属于此论坛的所有post。 我现在有这个: var v =(from forum in Forums join post in Posts on forum.ForumID equals post.Forum.ForumID select new { forum, //Need to retrieve all fields/columns from forum PostCount = //count all post that belong to this forum with a condition: count it only if post.Showit==1 } ).Distinct() 连接必须是左连接:如果没有属于某个论坛的post,则应检索论坛字段,但PostCount字段应为0。 结果集必须是不同的(连接给我完整的交叉…或者如何调用它)