Tag: entity framework

实体类型ApplicationUser不是当前上下文Asp.Net MVC的模型的一部分

以下是我的申请所需的代码。 我在我的帐户控制器文件中收到上述错误。 我在MVC 4中使用内置的帐户注册和登录代码。 无法注册用户。 我在stackoverflow上看到下面的链接解决我的错误但无法解决它 链接我跟着 我的Web中的连接字符串。配置代码 IdentityModel.cs文件 using System.Data.Entity; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; namespace PromoteMyName.Models { // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager […]

EF6 / SQL Server Compact,基于代码的配置

我正在尝试将我的myexe.exe.config配置从myexe.exe.config移动到代码,作为empty DbProviderFactories node in machine.config -issue empty DbProviderFactories node in machine.config的解决方法(此处描述: https ://stackoverflow.com/a/24273922/600559)。 我不想更改machine.config文件。 我已经阅读了基于代码的配置(EF6以后) 。 我尝试过这样的实现: https : //stackoverflow.com/a/23130602/600559 ,但我无法让它工作。 有没有人有一个有效的EF6 / SQL CE /基于代码的配置解决方案? 这是我的更改(从工作的.config解决方案到基于代码的解决方案):添加了新类: public class DatabaseConfiguration : DbConfiguration { public DatabaseConfiguration() { SetExecutionStrategy(“System.Data.SqlServerCe.4.0”, () => new DefaultExecutionStrategy()); SetProviderFactory(“System.Data.SqlServerCe.4.0”, new SqlCeProviderFactory()); SetProviderServices(“System.Data.SqlServerCe.4.0”, SqlCeProviderServices.Instance); } } 然后删除.config文件中的system.data和entityFramework节点。 现在这个工作,但是读取了machine.config文件:如果在machine.config有 ,我会得到以下exception: 所以真正的问题不是基于代码的配置不起作用,问题是仍在读取machine.config配置并导致问题。 谁知道怎么解决这个问题?

使用Entity Framework的StructuralTypeConfiguration.Ignore()忽略除指定集之外的所有属性

在EF CodeFirst Fluent API中,我可以这样编写: modelBuilder.Entity() .Ignore(e => e.Property1); modelBuilder.Entity() .Ignore(e => e.Property2); 如何忽略所有的属性,但一小部分,如下: modelBuilder.Entity() .IgnoreAllBut(e => e.ID, e => e.Important); 是否可以编写像这样的IgnoreAllBut扩展方法?

ASP.NET Core – 自定义AspNetCore.Identity实现不起作用

我正在构建一个完全自定义的AspNetCore.Identity实现,因为我希望TKey全面成为System.Guid 。 尊敬的我,我已经派出类型…… Role : IdentityRole RoleClaim : IdentityRoleClaim User : IdentityUser UserClaim : IdentityUserClaim UserLogin : IdentityUserLogin UserRole : IdentityUserRole UserToken : IdentityUserToken ApplicationDbContext : IdentityDbContext ApplicationRoleManager : RoleManager ApplicationRoleStore : RoleStore ApplicationSignInManager : SignInManager ApplicationUserManager : UserManager **ApplicationUserStore** : UserStore ApplicationUserStore是问题孩子! 履行 namespace NewCo.Identity { using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using System; public sealed class Role […]

如何在Visual Studio 2015中管理EF 6迁移?

我使用Visual Studio 2013最新更新使用EntityFramework -Version 6.1.2启动了一个新的MVC项目。 我进行了几次迁移并更新了数据库。 在此之后,我在另一台计算机上检出了该项目,并使用Visual Studio 2015 CTP 6打开。 如果我进入包管理器控制台并尝试运行任何迁移命令,则无法识别它们: add-migrations:术语“add-migrations”不被识别为cmdlet,函数,脚本文件或可操作程序的名称。 检查名称的拼写,或者如果包含路径,请validation路径是否正确,然后重试。 如何在Visual Studio 2015中使用Entity Framework 6管理迁移?

允许空外键GUID

我想创建一个类似GUID的可空外键,如下所示 [ForeignKey(“CreatedBy”)] [Display(Name = “Created by”)] public Guid? CreatedById { get; set; } public virtual User CreatedBy { get; set; } 但是当我添加迁移并更新数据库时,它不会使它在SQL中的表设计中允许为null。 还有另一种方法可以让它首先通过模型允许null吗?

Entity Framework在哪里存储属性名称与它在SQL中选择的列之间的映射?

我正在尝试使用ObjectQuery的ToTraceString()在EF(4.3)上构建一些自定义扩展,以从LINQ代码生成原始SQL。 我注意到在某些情况下,SQL中列的名称与查询元素类型的属性名称匹配,在其他情况下,它们被命名为C1,C2等。这使我很难做任何事情使用生成的SQL。 有谁知道如何将这些列名称映射回属性名称(我只关心能够为平面元组执行此操作,如匿名类型,如果有帮助,我不需要任何分层)。 依赖于私人/内部reflection的解决方案是完全可以接受的。 我知道我可能需要在将来的EF版本中对其进行调整。 此外,我只使用MS SqlServer(2008+),如果它有帮助。

entity framework5.0b2代码优先:同一个表的一对多和一对一,WITH Cascade Delete

经过几个小时的试验和错误,我到达了这个线程 ,解释了如何建立一对多关系和一对一关系与相同的两种类型。 但是,我无法使用Cascade Delete: 抛出:“无法确定依赖操作的有效排序。由于外键约束,模型要求或存储生成的值,可能存在依赖关系。” (System.Data.UpdateException)exception消息=“无法确定依赖操作的有效排序。由于外键约束,模型要求或存储生成的值,可能存在依赖关系。”,Exception Type =“System.Data.UpdateException “ 只有当我没有取消设置1:1关系时才会发生这种情况(参见下面的代码),我认为这会产生无效的引用。 我只是想知道是否有更好的方式来表示这一点。 示例代码: class Program { static void Main(string[] args) { Database.SetInitializer(new DropCreateDatabaseAlways()); using (var ctx = new Context()) { var user = new User(); ctx.Users.Add(user); ctx.SaveChanges(); var source = new PaymentSource(); user.PaymentSources = new Collection(); user.PaymentSources.Add(source); user.DefaultPaymentSource = source; ctx.SaveChanges(); // if I don’t do […]

entity framework包括filter子集合

我在LINQ查询中为包含的项添加一些过滤条件有些困难。 我的查询是这样的 var item = _Context.Order.Include(“Inner”) .Include(“Inner.first”) .Include(“Inner.second”) .Where(x => ( !(x.IsDeleted) && (x.IsActive) && (x.itemid == id))).FirstOrDefault(); 在上面的代码中,“Inner”是另一个项目列表。 现在我需要过滤内部项目。 我只需要内部项目,过滤条件为inner.isDeleted = true。 查询应该返回一个类, public class Order { public string Name { get; set; } public List Inner{ get; set; } public bool IsDeleted { get; set; } } 和InnerDetails类一样 public class InnerDetails { public […]

entity framework无法识别的唯一密钥

我有两个表, Reports和Visualizations 。 Reports有一个字段VisualizationID ,它通过外键指向Visualization的同名字段。 它还具有在该字段上声明的唯一键。 VisualizationID不可为空。 这意味着关系必须为0..1到1,因为每个 Reports记录必须具有与之关联的唯一的非空Visualizations记录。 entity framework不会这样看待它。 我收到以下错误: Error 113: Multiplicity is not valid in Role ‘Report’ in relationship ‘FK_Reports_Visualizations’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. 这有什么问题? 如何让EF识别正确的关系多样性?