我正在尝试先进行代码迁移以打开文件流,这样就不必手动完成。
我当然可以在数据库级别配置文件流(虽然我无法在服务上设置设置...)
但我需要执行这样的事情
ALTER DATABASE DBNAME
Run Code Online (Sandbox Code Playgroud)
添加文件组 FILESTREAMGroupName
包含文件流
所以我需要获取我正在使用的数据库的名称。
一旦我能得到它,然后我就可以通过查找文件来找出主 MDF 的路径,然后将文件流组的路径设置为我的下一个命令,但我无法弄清楚如何在其中获取数据库名称数据库迁移。
想法?
我在我的域中有一个类似于以下的一对多关系:
public class Movie
{
public int MovieID { get; set; }
public string MovieName { get; set; }
public virtual ICollection<MovieCategory> Categories { get; set; }
}
public class MovieCategory
{
[Key, ForeignKey("Movie")]
public int MovieID { get; set; }
[Key, ForeignKey("Category")]
public int CategoryID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime DateCreated { get; set; }
public virtual Movie Movie { get; set; }
public virtual Category Category { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过GETDATE()插入新电影(并在一堆复选框中检查某些类别)在 SQL 中设置 …
我刚刚开始使用ASP.net 5,MVC6和Entity Framework 7开展实验项目.我的ASP.Net Identity工作正常,但后来尝试将我自己的一些数据添加到DbContext并解决了这个问题.EF7报道:
处理请求时发生未处理的异常.
InvalidOperationException:未配置任何数据库提供程序.在设置服务时,通过在DbContext类或AddDbContext方法中覆盖OnConfiguring来配置数据库提供程序.
Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)堆栈查询Cookie标头
InvalidOperationException:未配置任何数据库提供程序.在设置服务时,通过在DbContext类或AddDbContext方法中覆盖OnConfiguring来配置数据库提供程序.
这是我的配置方法:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddTransient<ApplicationUnitOfWork>(instance => new ApplicationUnitOfWork());
}
Run Code Online (Sandbox Code Playgroud)
ApplicationUnitOfWork是一个超过EF7的外观,以减少紧耦合.到目前为止,这是整个事情:
public class ApplicationUnitOfWork : IUnitOfWork
{
readonly ApplicationDbContext dbContext;
public ApplicationUnitOfWork()
{
dbContext = new ApplicationDbContext();
Products = new ProductRepository(dbContext);
}
public void Dispose() { dbContext.Dispose(); }
public IUserRepository Users { get; }
public …Run Code Online (Sandbox Code Playgroud) 我正在为一个项目建立一个模型,除了下面的更改外,一切都按预期进行。我认为将类型指定为 asType而不是其描述为string.
namespace DataBase.Entities
{
public class Lock
{
public Guid Id { get; set; }
public DateTime? Occasion { get; set; }
public int Counter { get; set; }
public Type Entity { get; set; }
//public string Entity { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
你猜怎么着!EF有点不喜欢它。添加显式迁移时出现的错误如下,我不知道为什么。
System.ArgumentNullException:值不能为空。
参数名称:entitySet
大多数搜索导致人们发现 POCO 类有一些他们忘记的继承。有人建议强制重新启用迁移。我在模型中根本没有任何继承,强制迁移只提供了重新创建的配置文件。
我正在使用Entity Framework代码优先方法.我收到以下错误,当我运行Update-Database -script在一个空数据库,运行程序后enable-migrations和add-migration:
standardvba.DataAccessLayer.LessonQuestion :: EntityType'LessonQuestion'没有定义键.定义此EntityType的键.
LessonQuestions:EntityType:EntitySet'LessonQuestions'基于没有定义键的类型'LessonQuestion'.
LessonQuestion.cs:
public partial class LessonQuestion
{
public LessonQuestion()
{
this.LessonQuestionDetails = new List<LessonQuestionDetail>();
}
[Key]
public int QuestionID; // Key is defined
public int OrderNumber;
[ForeignKey("ParentQuestion")]
public int ParentQuestionID;
[ForeignKey("Lesson")]
public int LessonID { get; set; }
[ForeignKey("ActionedBy")]
public int ActionedByUserID { get; set; }
[MaxLength(1000)]
public string Question { get; set; }
public bool IsApproved { get; set; }
[Required] // Sets the CASCADE constraint, …Run Code Online (Sandbox Code Playgroud) 我添加了一个简单的类MemberModel,并将以下内容添加到我的数据库上下文类中:
public DBSet<MemberModel> Member { get; set; }
Run Code Online (Sandbox Code Playgroud)
添加迁移并更新数据库后,生成的表名称为Memberand not Members。我哪里做错了?
tl;dr:我有一个应该是的列,NVARCHAR(MAX)但是用脚手架进行迁移Add-Migration给了我一个最大长度为 4000 in 的列Up()。我必须做什么才能做到这一点MAX?
考虑以下模型:
public class Foo
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Memo { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在以下地方建立了两个约定Context.OnModelCreating:
将任何未配置的字符串属性的默认 maxLength 设置为 1024 的约定,其中包含以下行:
Properties<string>().Configure(c => c.HasMaxLength(1024));
Run Code Online (Sandbox Code Playgroud)基于属性的约定,读取DataTypeAttribute并相应地设置一些属性,即列类型和长度:
switch (attribute.DataType)
{
case DataType.MultilineText:
configuration.HasColumnType("nvarchar").IsMaxLength();
break;
case DataType.EmailAddress:
configuration.HasColumnType("nvarchar").HasMaxLength(255);
break;
}
Run Code Online (Sandbox Code Playgroud)我在设置迁移之前测试了这一切,它工作得很好,给了我下表 - …
如您所知,样板不提供实体类来添加列,我想在名为(AbpUser)的表中添加列。我尝试在迁移中开设一门课程,如下所示
public partial class AddRatingMig : DbMigration
{
public override void Up()
{
AddColumn("dbo.AbpUsers", "UserType", c => c.String());
}
public override void Down()
{
DropColumn("dbo.AbpUsers", "UserType");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在 Pm 控制台中运行命令 Update-Database 。但没有成功。如您所知,样板不提供实体类来编辑列。请帮忙提前致谢
asp.net asp.net-mvc ef-code-first asp.net-boilerplate aspnetboilerplate
c# ×4
asp.net-mvc ×2
entity-framework-migrations ×2
asp.net ×1
asp.net-core ×1
ef-core-3.0 ×1
key ×1
poco ×1