当我PM> Remove-Migration -context BloggingContext使用EF Core在VS2015中运行ASP.NET Core项目时,我收到以下错误:
System.InvalidOperationException: The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration. at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has …Run Code Online (Sandbox Code Playgroud) c# entity-framework-core visual-studio-2015 .net-core asp.net-core
我有 2 个实体,它们是一对多相关的
public class Restaurant {
public int RestaurantId {get;set;}
public string Name {get;set;}
public List<Reservation> Reservations {get;set;}
...
}
Run Code Online (Sandbox Code Playgroud)
public class Reservation{
public int ReservationId {get;set;}
public int RestaurantId {get;set;}
public Restaurant Restaurant {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用我的 api 预订餐厅
var restaurants = await _dbContext.Restaurants
.AsNoTracking()
.AsQueryable()
.Include(m => m.Reservations).ToListAsync();
.....
Run Code Online (Sandbox Code Playgroud)
我收到错误响应,因为对象包含彼此的引用。有相关帖子建议创建单独的模型 或添加NewtonsoftJson 配置
问题是我不想创建单独的模型,第二个建议没有帮助。有没有办法在没有循环关系的情况下加载数据?*
System.Text.Json.JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 32。 at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth) at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer , Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, …
我找不到为我的field属性添加唯一约束的方法
public class User
{
[Required]
public int Id { get; set; }
[Required]
// [Index("IX_FirstAndSecond", 2, IsUnique = true)] not supported by core
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在用
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
Run Code Online (Sandbox Code Playgroud) 在EF6中,我们通常能够使用这种方式配置实体.
public class AccountMap : EntityTypeConfiguration<Account>
{
public AccountMap()
{
ToTable("Account");
HasKey(a => a.Id);
Property(a => a.Username).HasMaxLength(50);
Property(a => a.Email).HasMaxLength(255);
Property(a => a.Name).HasMaxLength(255);
}
}
Run Code Online (Sandbox Code Playgroud)
我们如何在EF Core中做,因为当我继承EntityTypeConfiguration类时无法找到该类.
我从GitHub下载EF Core原始源代码,我找不到它.有人可以帮忙吗?
升级到ASP.NET Core 2.0后,我似乎无法再创建迁移.
我越来越
"在类'Program'上调用方法'BuildWebHost'时发生错误.继续没有应用服务提供者.错误:发生了一个或多个错误.(无法打开登录请求的数据库"...".登录失败.登录用户'...'失败
和
"无法创建'MyContext'类型的对象.将'IDesignTimeDbContextFactory'的实现添加到项目中,或者参见 https://go.microsoft.com/fwlink/?linkid=851728以获取在设计时支持的其他模式."
我之前运行的命令是$ dotnet ef migrations add InitialCreate --startup-project "..\Web"(来自带有DBContext的项目/文件夹).
连接字符串: "Server=(localdb)\\mssqllocaldb;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true"
这是我的Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Run Code Online (Sandbox Code Playgroud) c# entity-framework-core asp.net-core-mvc asp.net-core asp.net-core-2.0
使用Entity Framework Core删除dbData.Database.SqlQuery<SomeModel>我无法找到为我的全文搜索查询构建原始SQL查询的解决方案,该查询将返回表数据以及排名.
我见过在Entity Framework Core中构建原始SQL查询的唯一方法是通过dbData.Product.FromSql("SQL SCRIPT");它没有用,因为我没有DbSet来映射我在查询中返回的排名.
有任何想法吗???
我是EF核心的新手,我正试图让它与我的asp.net核心项目一起工作.
startup.cs在尝试配置dbcontext以使用来自config的连接字符串时,我得到了上述错误.我正在关注本教程:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro
startup.cs中有问题的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;
namespace tracV2
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
string conn = Configuration.GetConnectionString("optimumDB");
services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
}
Run Code Online (Sandbox Code Playgroud)
usesqlserver如果我将其直接放入上下文中,则会识别该方法:
using System; …Run Code Online (Sandbox Code Playgroud) 我的应用程序被移植到.NET核心将使用新的EF核心与SQLite.我想在首次运行应用程序时自动创建数据库和表结构.根据EF核心文档,这是使用手动命令完成的
dotnet ef migrations add MyFirstMigration
dotnet ef database update
Run Code Online (Sandbox Code Playgroud)
但是,我不希望最终用户输入这些命令,并且希望让应用程序创建并设置数据库以供首次使用.对于EF 6,有类似的功能
Database.SetInitializer(new CreateDatabaseIfNotExists<MyContext>());
Run Code Online (Sandbox Code Playgroud)
但在EF Core中,这些似乎并不存在.我找不到任何关于EF核心等效的示例或文档,并且在EF核心文档的缺失功能列表中没有提到.我已经设置了模型类,所以我可以编写一些代码来基于模型初始化数据库,但如果框架自动执行此操作,则会更容易.我不想自动构建模型或迁移,只需在新数据库上创建表结构.
我在这里遗漏了什么或者在EF核心中缺少自动创建表功能吗?
假设我们有这个模型:
public class Tiers
{
public List<Contact> Contacts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class Contact
{
public int Id { get; set; }
public Tiers Tiers { get; set; }
public Titre Titre { get; set; }
public TypeContact TypeContact { get; set; }
public Langue Langue { get; set; }
public Fonction Fonction { get; set; }
public Service Service { get; set; }
public StatutMail StatutMail { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用EF7,我想从Tiers表中检索所有数据,包括Contact表中的数据,Titre表中的数据,TypeContact表中的数据等等......只需一条指令.使用Include/ThenInclude API,我可以编写如下内容:
_dbSet
.Include(tiers => …Run Code Online (Sandbox Code Playgroud) 我正在尝试为调用异步存储库的类创建单元测试.我正在使用ASP.NET Core和Entity Framework Core.我的通用存储库看起来像这样.
public class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : class
{
private readonly SaasDispatcherDbContext _dbContext;
private readonly DbSet<TEntity> _dbSet;
public EntityRepository(SaasDispatcherDbContext dbContext)
{
_dbContext = dbContext;
_dbSet = dbContext.Set<TEntity>();
}
public virtual IQueryable<TEntity> GetAll()
{
return _dbSet;
}
public virtual async Task<TEntity> FindByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public virtual IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
{
return _dbSet.Where(predicate);
}
public virtual void Add(TEntity entity)
{
_dbSet.Add(entity);
}
public virtual void Delete(TEntity entity)
{
_dbSet.Remove(entity);
}
public virtual …Run Code Online (Sandbox Code Playgroud)