我知道这可以在单个属性上使用Fluent API来完成,但我希望在我的模型中使用.NET DateTime的任何地方都能自动执行此操作.以下是执行单个属性的示例:
modelBuilder.Entity<T>()
.Property(f => f.MyDateTimeProperty)
.HasColumnType("datetime2");
但是我不希望它只在这一个属性上自动发生.在Model First中有一个修复,通过编辑T4生成模板,所以我知道它可以在那里完成但我在Code First中需要相同的东西.
我正在使用ASP.NET Identity 2.0和EF 6.1.1构建MVC5应用程序.
这是我当前的Login方法的一部分:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
return View(model);
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user == null)
break;
if (!UserManager.IsInRole(user.Id, "OfficeUser"))
break; // << I always hit this line - even if the user is an OfficeUser
Run Code Online (Sandbox Code Playgroud)
这个工作正常,直到我点击UserManager.IsInRole().这总是返回false.
在某处我读到IsInRole()如果相应的用户没有登录就会失败.但是因为我传递了SignInManager.PasswordSignInAsync(),我相信这应该没问题.
是的,我已经多次检查数据库并且非常小心;)我的测试用户和我的测试角色"OfficeUser"肯定是相互分配的.
有人有想法吗?
我创建了一个新的MVC 5项目,当我在Package Manager控制台中获得以下PS异常时,我尝试为我创建的Code First实体启用迁移:
One or more validation errors were detected during model generation:
Project.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
Project.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)
问题是我的项目中没有调用IdentityUserRole或没有类IdentityUserLogin.这些名称位于Microsoft.AspNet.Identity.EntityFramework命名空间中的类,但错误是说这些类存在于我的项目命名空间中.
我该如何解决这个问题?我已经多次清理并重新编译了该项目,似乎没有什么能解决这个问题.
如果我删除我的 …
c# entity-framework ef-code-first ef-migrations entity-framework-6
我正在尝试使用EF代码6.1.2为我的数据库访问逻辑添加一些集成测试.我想在一个单独的数据库上执行此操作,这样我就不会在测试数据中混淆生产数据库.
此外,测试应该是可重复的,这意味着应该创建数据库(如果不存在),种子测试数据,运行测试并最终在完成后删除它.
如果没有Enable-Migrations测试数据库的命令,我也不知道如何做到这一点.
这将是我dbContext的生产:
public partial class ApplicationDbContext :
IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IApplicationDbContext
{
public ApplicationDbContext() : base("name=DefaultConnection") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (base.Database.Connection.ConnectionString == "DefaultConnectionTest")
Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
else
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
modelBuilder.Entity<ApplicationRole>().ToTable("Roles");
modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRoles");
modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
}
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Person> Persons { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我把一切都建立了使用Dependency Injection从Ninject.使用连接字符串作为参数添加额外的构造函数public ApplicationDbContext(string …
c# sql-server integration-testing entity-framework ef-code-first
此代码导致NotSupportedException.
var detailList = context.Details.Where(x => x.GetType().GetProperty("Code").GetValue(x,null).ToString() == "00101").ToList();
Run Code Online (Sandbox Code Playgroud)
但是这段代码可行.
var detailList = context.Details.AsEnumerable().Where(x => x.GetType().GetProperty("Code").GetValue(x,null).ToString() == "00101").ToList();
Run Code Online (Sandbox Code Playgroud)
MSDN说:
- AsEnumerable()返回输入为IEnumerable的输入
那么为什么我们需要使用AsEnumerable()方法呢?
我在服务器上有一个mysql数据库,我已经买了一些空间.现在我想在我的网站上添加一个新的mvc应用程序来尝试一些东西,而不会弄乱我现有的应用程序.这让我有点问题,因为我只有一个数据库可用,我可以买一个新的,但我不想每次想要尝试填充时都要买一个新数据库.
所以我想,为什么不只是为我的所有表预先修复该应用程序的一些唯一值,这样我可以保持存储的数据分开,同时仍然使用相同的数据库.这让我在stackoverflow和网络上的其他地方浏览了无数篇文章.大约一天后,我偶然发现了这个金块
<!-- language: c# -->
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(entity => entity.ToTable("pf_" + entity.ClrType.Name));
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
只要您使用mssql数据库,或者删除mysql数据库并创建一个新数据库,这就完全符合我的要求.但是,我给出的数据库不是mssql数据库,丢弃它不是一个选项.
当使用现有的mysql db时,我尝试运行
update-database
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Can't find file: '.\mytestdb\dbo@002etestitems.frm' (errno: 2 - No such file or directory)
Run Code Online (Sandbox Code Playgroud)
其中"mytestdb"是我正在测试的本地数据库,"testitems"是当前/上一个表名,.frm是db格式的,我不知道这个"dbo @ 002e"是什么,我试过googling它没有用.
我的堆栈跟踪可以在这里找到. http://pastebin.com/yF2dGkm6
有没有人处于类似的情况,或者有关于如何使其工作的想法?=)
我有2个模型,其中第二个模型继承自第一个模型并具有其他属性:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ItemMap : EntityTypeConfiguration<Item>
{
public ItemMap()
{
this.ToTable("items");
this.Map(m => m.Requires("type")
.HasValue("Type1")
.IsRequired());
this.HasKey(t => t.Id).Property(t => t.Id).HasColumnName("id")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Other properties here...
}
}
public class SecondItem : Item
{
public string Other { get; set; }
}
public class SecondItemMap : EntityTypeConfiguration<SecondItem>
{
public SecondItemMap()
{
this.Map(m => m.Requires("type")
.HasValue("Type2")
.IsRequired());
//Other properties here...
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我得到以下异常: …
我们的客户可以访问没有定义主键的视图.我知道实体框架需要一个主键来表识别.
但是对于没有主键的视图,仍然可以查询.
我试图找到但总是实体框架给出错误说:
错误:: EntityType'ViewWeight'没有定义键.定义此EntityType的键.
我理解密钥对于表很重要,但对于只是为了读取的视图,有任何黑客或方法来读取值而不修改视图本身.
我FileTable先在项目中使用SQL Server 2014和EF代码。
当我使用此命令时
USE [master]
GO
ALTER DATABASE [OnlineStore]
SET FILESTREAM( DIRECTORY_NAME = N'OnlineStore',
NON_TRANSACTED_ACCESS = FULL) WITH NO_WAIT
GO
Run Code Online (Sandbox Code Playgroud)
它在sql中显示此警告
当FILESTREAM数据库选项NON_TRANSACTED_ACCESS设置为FULL且READ_COMMITTED_SNAPSHOT或ALLOW_SNAPSHOT_ISOLATION选项处于启用状态时,T-SQL和FILETABLE上下文中对FILESTREAM数据的事务性读取访问将被阻止。
现在我继续创建表,并插入文件夹和文件没问题。
我读取数据的问题,当读取数据时显示此错误:
消息33447,级别16,状态1,第2行
无法访问FileTable'File'中的file_stream列,因为FileTable不支持行版本控制。可以将事务级别设置为“ READ COMMITTED SNAPSHOT”或“ SNAPSHOT”以外的值,或者使用READCOMMITTEDLOCK表提示。
我先使用EF代码-如何解决此问题?
预先感谢您的帮助.我对使用Entity Framework 6include()方法时遇到的情况感到有些困惑.据我所知,include方法的作用就像封闭的对象一样,当对象匹配时也是如此.LEFT JOINNULLOUTER JOIN
我将通过发生在我身上的例子,以便你帮我理解发生了什么.
我的表有以下模型:
public class Booking
{
[Key]
public int ID{ get; set; }
public string Description{ get; set; }
public decimal Amount{ get; set; }
public decimal AmoutPaid{ get; set; }
public DateTime? Checkin { get; set; }
public DateTime? Checkout { get; set; }
[ForeignKey("SourceBooking ")]
public int SourceBookingId { get; set; }
public SourceBooking SourceBooking { get; set; }
}
public class SourceBooking
{
[Key]
public int …Run Code Online (Sandbox Code Playgroud) ef-code-first ×10
c# ×5
asp.net-mvc ×1
filetable ×1
lambda ×1
left-join ×1
linq ×1
mysql ×1
oracle ×1
sql ×1
sql-server ×1
view ×1