Tag: entity framework

如何确定PropertyType是否为外键

我有以下类’schakeling’,用EF生成,表示数据库表’schakeling’。 在数据库中,’id’是主键,’plc_id’是外键。 public partial class schakeling { public schakeling() { this.verbruik = new HashSet(); } public int id { get; set; } public int plc_id { get; set; } public string var_output { get; set; } public string omschrijving { get; set; } public int vermogen { get; set; } public Nullable status { get; […]

如何通过EF迁移将int ID列更改为Guid?

我正在使用EF代码优先方法,并希望将Id字段更改为guid但似乎无法通过以下错误。 这是我的第一次迁移: public partial class CreateDownloadToken : DbMigration { public override void Up() { CreateTable( “dbo.DownloadTokens”, c => new { Id = c.Int(nullable: false, identity: true), FileId = c.Int(), UserId = c.String(nullable: false, maxLength: 128), ValidUntil = c.DateTime(nullable: false), }) .PrimaryKey(t => t.Id) .ForeignKey(“dbo.Files”, t => t.FileId) .ForeignKey(“dbo.Users”, t => t.UserId, cascadeDelete: true) .Index(t => […]

entity framework – 基础提供程序在ConnectionString上失败

在使用entity framework时,我已将其拆分为自己的项目: RivWorks.Model – 包含实体模型 RivWorks.Controller – 使用实体模型并包含商业规则 RivWorks.View.Web – 该网站 RivWorks.View.Services – WCF项目 网站上的所有内容都运行良好。 我可以调用Controller并获得有效的模型。 当我从Web.Service尝试相同的事情时,我收到此错误: 错误: ConnectionString上的底层提供程序失败。 堆栈跟踪: at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) 在System.Data.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString) at System.Data.Objects.ObjectContext..ctor(String connectionString,String defaultContainerName) 在RivWorks.Model.Entities.RivFeedsEntities1..ctor(String connectionString) 在RivWorks.Model.FeedStoreReadOnly..ctor(String connectionString) 在RivWorks.Controller.ProductManager.LookupProduct(String productID,String sku,String urlRef,String env,String logPath) 我有点困惑为什么和挖掘错误日志我终于弄清楚连接字符串没有被从网站的配置文件中读取。 所以,我添加了一些代码来捕获它,并且,现在,硬编码值。喜欢: public dataObjects.NegotiateSetup LookupProduct(string productID, string sku, string urlRef, string env, string […]

Foreach Razor里面的Foreach

我正在尝试编写一个foreach循环,它将找到每个不同的类别类型,然后列出具有该类别分配的每个Title。 例如: @model IEnumerable @{ ViewBag.Title = “Home”; } @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.ProgramType.ProgramType) foreach (var listing in Model) { @Html.DisplayFor(modelItem => listing.ProgramTitle) } } 调查回复模型: public class SurveyProgramModel { [Key] public int ProgramId { get; set; } public int ProgramYear { get; set; } public int ProgramStatusId { get; set; […]

C#Linq字符串与indexOf进行比较

我有一个表,其中包含一个包含主机名的字符串字段。 它们大多是完全合格的域名,但多年来第一个“点”之后的域位已经改变,因为各种DNS变化已经落在我们身上。 所以我可能将表中的机器“tom”作为: tom.company.com tom.it.company.com tom.newComapnyBranding.com … 我经常要对“当前”主机名和这个历史商店进行比较。 做类似的事情: WHERE UPPER(SUBSTRING(@foo, 1, CHARINDEX(“.”, @foo))) = UPPER(SUBSTRING(myDB.myTable.machineName, 1, CHARINDEX(“.”, myDB.myTable.machineName))) 好吧,我试图将其中一个转换为Linq查询,但我在“索引”部分磕磕绊绊。 我差一点接近: myTable.machineName.ToUpper().Substring(0, myTable.machineName.IndexOf(“.”)) .Equals(foo.ToUpper().Substring(0, foo.IndexOf(“.”))) 但是visual studio正在抱怨“IndexOf”。 它声称我需要将IndexOf更改为: IndexOf(“.”, StringComparison.Ordinal)) 但是当我运行它时,我收到exception消息: LINQ to Entities does not recognize the method ‘Int32 IndexOf(System.String, System.StringComparison)’ method, and this method cannot be translated into a store expression 你如何在Linq中使用这种基于索引的子字符串?

如何使用AutoMapper将目标对象与源对象中的子对象映射?

我有这样的源和目标对象: class ProductWithCategories // Source class { public Product Product { get; set; } // Product is an EF entity class public IEnumerable Categories { get; set; } } class ProductViewModel // Dest class { public int Id { get; set; } // Other properties with the same name as Product class public IEnumerable Categories […]

未找到entity framework方法; 版本问题?

我正在研究另一个开发人员构建的系统。 当我运行该项目时,我在运行时收到以下错误。 这是一个entity framework方法吗? 它来自特定版本吗? 我不确定如何追查此错误的来源。 System.MissingMethodException:找不到方法:’Void System.Data.Entity.ModelConfiguration.EntityTypeConfiguration`1.ToTable(System.String)’。

无法在Entity Framework中使用AddRange自动生成IDENTITY

我不知道它是entity framework的设计选择还是代表我的错误方法,但每当我尝试将AddRange实体添加到DbSet时,我似乎无法获得自动生成的IDENTITY字段。 [Table(“entities”)] public class Entity { [Key] [Column(“id”)] public long Id { get; set; } [Column(“field”)] public string Field { get; set; } } var entities = new Entity[] { new Entity() { Field = “A” }, new Entity() { Field = “B” }, }; _dbContext.Entities.AddRange(entities); await _dbContext.SaveChangesAsync(); //ids are still default(long) at this […]

从Entity框架返回自定义对象并分配给对象数据源

我需要一些问题的指导,我正在使用Entity Framework 4.0,我有一个DAL和BLL并且在页面上绑定到ObjectDataSource。 我不得不使用PIVOT和动态SQL编写存储过程,以便按照我想要的方式从多个实体返回数据。 现在我想弄清楚如何让Entity Framework返回一个我可以绑定到页面上的ObjectDataSource的自定义对象,我需要使用自定义对象或动态对象,因为存储过程可以返回任意数量的列所以我不能使用强类型类或实体,我还需要能够将它与ObjectDataSource绑定。 有人能指出一个很好的方法来做这个以及如何定义我的function? 请提供一些代码示例。 我读到我应该尝试使用List返回一个对象,因为EF不支持返回数据表/数据集,到目前为止我有以下内容,但我知道这是不正确的。 我没有太多使用仿制药,如果你能指出如何做到这一点,我相信这对很多人都有帮助。 请提供该函数的代码示例以及如何将ObjectDataSource绑定到返回对象? 非常感谢您的帮助! 感谢您的帮助Richard根据您对使用DbDataRecord的建议,这就是我现在的function DAL中ObjectDataSource C#函数 public List GetData(int product_id) { List availableProducts = new List(); var groupData = context.ExecuteStoreQuery(“exec spGetProducts @ProductID={0}”, product_id); availableProducts = groupData.ToList(); return availableProducts; } ASPX页面中的ObjectDataSource 现在,当我访问页面时,我收到此错误: 结果类型“System.Data.Common.DbDataRecord”可能不是抽象的,必须包含默认构造函数 这是因为ExecuteStoreQuery期望被定义为类还是实体? 如何根据存储过程结果创建一个对象并将其分配给它?

具有可选参数的entity framework?

使用Entity Framework 5是否可以使用带有可选参数的存储过程,这样您就不必为每个未使用的参数添加空值? 我必须使用的存储过程有87个参数,其中只有2个是必需的。 我真的很讨厌在每个调用中放入85个空值的想法。