我必须将所有 IdentityFramework 类和接口移至单独的库,这意味着新的Common.Models.Interfaces接口库只能引用其他接口库(或 .net 库)。这意味着我所有的ApplicationUser参数和变量都变成了IApplicationUser。
经过几个小时的更改后,我已经全部构建完毕,但在运行时我收到以下错误:
当前类型 Microsoft.AspNet.Identity.IUserStore`1[Common.Models.Interfaces.Account.IApplicationUser] 是一个接口,无法构造。您是否缺少类型映射?
所以我的问题是:我可能会缺少什么类型映射?
我之前已经使用http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registrationdbcontext中的以下代码解决了问题(特别是使用)InjectionConstructor
container.RegisterType<IApplicationUser, ApplicationUser>();
container.RegisterType<DbContext, ApplicationDbContext>();
container.RegisterType<IdentityDbContext<ApplicationUser>, ApplicationDbContext>();
container.RegisterType<IIdentityMessageService, EmailService>();
container.RegisterType<IApplicationSignInManager, ApplicationSignInManager>();
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(typeof(ApplicationDbContext)));
Run Code Online (Sandbox Code Playgroud)
注意:此处提到的其他错误已删除,因为它具有误导性。看起来我需要额外的映射,但不知道是什么。
应用程序的用户界面和类如下(已修剪),以便您可以发现我出错的地方:
public interface IApplicationUser : IUser, IUser<string>
{
...
}
Run Code Online (Sandbox Code Playgroud)
public class ApplicationUser : IdentityUser, IApplicationUser
{
...
}
Run Code Online (Sandbox Code Playgroud)
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
Run Code Online (Sandbox Code Playgroud)
显然,不可能包含所有相关代码来重现此问题,因为它运行了 1000 行,并且不需要重现,因此只有 IOC 专家才应申请。:)
在获得有用的链接后,@guillaume31我尝试了以下操作:
container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>), new InjectionConstructor(typeof(ApplicationDbContext)));
Run Code Online (Sandbox Code Playgroud)
这可以编译,但我收到一个新错误:
GenericArguments[0], 'Common.Models.Interfaces.Account.IApplicationUser', on …
c# asp.net-mvc entity-framework unity-container asp.net-identity
我想更新实体框架中的多个列。我现在用这个:
var user = new Relations { Id=1, Status = 1, Date = DateTime.Now, Notification = 0 };
db.Relations.Attach(user);
db.Entry(user).Property(x => x.Status).IsModified = true;
db.Entry(user).Property(x => x.Notification).IsModified = true;
db.Entry(user).Property(x => x.Date).IsModified = true;
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
db.Entry(user).Property有没有更好的方法来更新列而不需要多次重复代码?
我知道问题所在:我缺少一个映射来帮助 EF 了解两个表之间的关系,但我没有看到我的错误在哪里。
我有一个使用 EF CF 7 的 Web Api。我可以很好地查询并获取父表数据,但是当我尝试包含相关子数据时,我收到无效列错误。
我的两个表如下所示:
public class Categories
{
private ICollection<SubCategories> _subcat;
public int Id { get; set; }
public string Category { get; set; }
public virtual ICollection<SubCategories> SubCategories
{
get { return _subcat ?? (_subcat = new List<SubCategories>()); }
set { _subcat = value; }
}
}
public class SubCategories
{
public int Id { get; set; }
public string SubCategory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
上面的类与我的数据库模式匹配,除了SubCategories 表有一列 CategoryId 并且它是父表 …
我使用 EF 开发框架。我想获取实体的所有被忽略的属性来构建一些特殊查询。我该怎么做?
public class Customer
{
public int Id { get; set; }
public DateTime BirthDate { get; set; }
public int Age { get; set; }
}
public class CustomerContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);
base.OnModelCreating(modelBuilder);
}
public DbSet<Customer> Customers { get; set; }
}
public static class DbContextExtensions
{
public static List<string> GetIgnoredProperties(this DbContext context, string entityTypeName)
{
// ???
}
}
Run Code Online (Sandbox Code Playgroud) c# entity-framework ef-code-first entity-framework-5 entity-framework-6
因此,当我尝试从 Web Api 获取数据时,就会发生这种情况:
异常消息:
LINQ to Entities 仅支持无参数构造函数和初始值设定项。
这是我的服务:
public async Task<ICollection<TicketDto>> GetAllUnansweredTicketsAsync()
{
return await _context.Tickets.Include(m => m.Message)
.Include(u=>u.User)
.OrderByDescending(d=>d.Message.Date)
.Select(t => new TicketDto(t)).ToListAsync();
Run Code Online (Sandbox Code Playgroud)
这就是我的 DTO 的样子:
public class TicketDto
{
public int ID { get; set; }
public string Username { get; set; }
public string Issue { get; set; }
public DateTime Date { get; set; }
public TicketDto(Ticket ticket)
{
ID = ticket.ID;
Username = ticket.User.UserName;
Issue = ticket.Message.Title;
Date = ticket.Message.Date;
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下如何解决这个问题吗?
我正在尝试计算两个日期之间的时间差,并想获取我的日期的日期名称。
例如:2016 年 4 月 7 日
这是我的课程:
public class Attendance
{
public int Id { get; set; }
public Nullable<System.DateTime> StartDateTime { get; set; }
public Nullable<System.DateTime> EndDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时:
var query = (from t in context.Attendance
select new
{
timeDiff=t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalHours,
Day=System.StartDateTime.ToString("dddd");
}).tolist();
Run Code Online (Sandbox Code Playgroud)
错误
LINQ to Entities 无法识别“System.TimeSpan Subtract(System.DateTime)”方法,并且此方法无法转换为存储表达式
我不想像下面那样做:
var query = (from t in context.Attendance.toList().
select new
{
timeDiff=t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalHours,
Day=System.StartDateTime.ToString("dddd");
}).tolist();
Run Code Online (Sandbox Code Playgroud)
我Datetime的表中存储了这样的格式,因此我希望预期的输出如下所示,在差异字段中显示:
startDatetime Enddatetime Difference
----------------------------------------------------------------
2016-06-29 15:52:32.360 2016-06-29 …Run Code Online (Sandbox Code Playgroud) 也许这是一个重复的线程,但我会尝试一下,因为存在微小的差异。
我正在尝试构建一个动态表达式来过滤集合属性。
代码:
public class TestEntity
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<string> Values { get; set; }
}
public class TestFilter
{
public TestFilter()
{
var itens = new List<TestEntity>();
itens.Add(new TestEntity { ID = 1, Name = "Test1", Values = new List<string> { "V1", "V2" } });
itens.Add(new TestEntity { ID = 2, Name = "Test2", Values = new List<string> { "V6", "V3" } });
itens.Add(new TestEntity { …Run Code Online (Sandbox Code Playgroud) 我读过的大部分内容(例如来自作者的内容)都表明应该使用 AutoMapper 将实体映射到 DTO。它不应该从数据库加载任何内容。
但如果我有这个怎么办:
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public class CustomerDto {
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<int> OrderIds { get; set; } // here is the problem
}
Run Code Online (Sandbox Code Playgroud)
我需要从 DTO 映射到实体(即从CustomerDto到Customer),但首先我必须使用该外键列表从数据库加载相应的实体。AutoMapper 可以使用自定义转换器来做到这一点。
我同意这感觉不对……但是还有什么选择呢?将该逻辑粘贴到控制器、服务、存储库、某些管理器类中?所有这些似乎都在将逻辑推向同一层的其他地方。如果我这样做,我还必须手动执行映射!
从 DDD …
我有一个看法
[Table("View1")]
public class View1Model
{
[Key]
public int Id { get; set; }
public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想向表中添加另一个 public int Weight,因为在 sql 数据库中我更新了视图,但是迁移不起作用。
它说当我尝试更新数据库时
无法更改“dbo.View1”,因为它不是表。
我知道我可以删除“table”属性,但这不起作用,因为我需要签入我的代码。这是不可能的
我希望这不是我一直在寻找的重复,但我更多地关注了这背后的原因。
我已经设置了一个用户对象。
public class User
{
public User()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
public Guid UserGuid { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public int CompanyID { get; set; }
public int StatusID { get; set; }
[ForeignKey("StatusID")]
public Status Status { get; set; }
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public UserRole UserRole { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和子对象
public …Run Code Online (Sandbox Code Playgroud) entity-framework ×10
c# ×9
asp.net-mvc ×2
linq ×2
ajax ×1
automapper ×1
datetime ×1
filtering ×1
lambda ×1
view ×1