我创建了一个新的干净的asp.net 5项目(rc1-final).使用身份验证我只需要具有以下代码的ApplicationDbContext.cs:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder builder)
{
// On event model creating
base.OnModelCreating(builder);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意ApplicationDbContext使用IdentityDbContext而不是DbContext.
有任何IdentityConfig.cs.我需要将经典受保护的覆盖void Seed用于创建角色和用户(如果它不存在)?
我想知道除了构建一个用于模拟的包装器之外还有什么方法FromSql吗?我知道这个方法是静态的,但是因为它们添加了类似于AddEntityFrameworkInMemoryDatabase实体框架核心的东西,我认为可能还有一个解决方案,我在我的项目中使用EF Core 1.0.1.
我的最终目标是测试此方法:
public List<Models.ClosestLocation> Handle(ClosestLocationsQuery message)
{
return _context.ClosestLocations.FromSql(
"EXEC GetClosestLocations {0}, {1}, {2}, {3}",
message.LocationQuery.Latitude,
message.LocationQuery.Longitude,
message.LocationQuery.MaxRecordsToReturn ?? 10,
message.LocationQuery.Distance ?? 10
).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我想确保我的查询是使用我传入其中的相同对象来处理的,基于实体框架6中的这个答案,我可以这样做:
[Fact]
public void HandleInvokesGetClosestLocationsWithCorrectData()
{
var message = new ClosestLocationsQuery
{
LocationQuery =
new LocationQuery {Distance = 1, Latitude = 1.165, Longitude = 1.546, MaxRecordsToReturn = 1}
};
var dbSetMock = new Mock<DbSet<Models.ClosestLocation>>();
dbSetMock.Setup(m => m.FromSql(It.IsAny<string>(), message))
.Returns(It.IsAny<IQueryable<Models.ClosestLocation>>());
var contextMock = new Mock<AllReadyContext>();
contextMock.Setup(c => c.Set<Models.ClosestLocation>()).Returns(dbSetMock.Object); …Run Code Online (Sandbox Code Playgroud) 我有两个动作,一个是带有此签名的HttpGet:
[Route("NewsLetter/SelectEmail/{page?}")]
[HttpGet]
public ActionResult SelectEmail(int? page, string priCat, string secCat)
{
...
}
Run Code Online (Sandbox Code Playgroud)
还有一个HttpPost有这个签名:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectEmail(int id)
{
...
}
Run Code Online (Sandbox Code Playgroud)
在为HttpGet方法设置上述路由之后,我注意到另一个方法HttpPost已停止工作,在挖掘之后我已经意识到路由HttpGet也为自己设置了HttpPost,直到我明确设置路由属性它才起作用为了它:
[Route("NewsLetter/SelectEmail/{id}")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectEmail(int id)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想知道,这是一个错误吗?如果不是,那么无论如何设置路由属性[HttpGet]而不影响相应的[HttpPost]?
我正在使用实体框架7在我的Asp.Net 5项目中设置我的数据库,以前使用EF 6,当我想让我的一些列可以为空时,我会使用:
modelBuilder.Entity<Article>().Property(t => t.ArticleDateModified).IsOptional();
Run Code Online (Sandbox Code Playgroud)
但似乎IsOptional不再是EF7的一部分,我想知道如何使用EF7实现同样的目标?
编辑:
马克的答案确实是正确的,首先我虽然它有效,因为我找到了类似的东西IsOptional:
builder.Entity<Article>().Property(t => t.ArticleDateModified).IsRequired(false);
Run Code Online (Sandbox Code Playgroud)
但是在没有它的情况下运行一些测试后,它将数据库列设置为可空,因为我在域模型中将其标记为可为空:
public DateTime? ArticleDateModified { get; set; }
Run Code Online (Sandbox Code Playgroud)
另外值得注意的是,当我使用DateTime非可空并使用时IsRequired(false),我收到以下错误:
实体类型"Article"上的属性"ArticleDateModified"不能标记为nullable/optional,因为属性的类型是"DateTime",它不是可空类型.任何属性都可以标记为不可为空/必需,但只有可空类型的属性和不属于主键的属性可以标记为可空/可选.
所以,我想知道在IsRequired(false)这里有什么用,如果我要做的一件事就是让数据库列可以为空,那么在我的域类中它是否可以为空?
类上有两种方法AuthenticationManager,ForbidAsync()而且ChallengeAsync(),我知道我可以在我的控制器中执行HttpContext.Authentication.ForbidAsync或返回类型的结果,ForbidResult它具有相同的效果,同样如此ChallengeAsync.但它们似乎产生了相同的结果:
public ForbidResult ForbidResult()
{
return Forbid();
}
public ChallengeResult ChallengeResult()
{
return Challenge();
}
Run Code Online (Sandbox Code Playgroud)
目前没有太多关于它们的使用或任何示例的文档,我想知道如何以及为何使用它们.
更新:顺便说一下,我在这个领域的研究报告中提到了一篇名为Asp.Net Core Action Results Explained的文章.
我有一个ArticleComment实体,如下所示:
public class ArticleComment
{
public int ArticleCommentId { get; set; }
public int? ArticleCommentParentId { get; set; }
//[ForeignKey("ArticleCommentParentId")]
public virtual ArticleComment Comment { get; set; }
public DateTime ArticleDateCreated { get; set; }
public string ArticleCommentName { get; set; }
public string ArticleCommentEmail { get; set; }
public string ArticleCommentWebSite { get; set; }
public string AricleCommentBody { get; set; }
//[ForeignKey("UserIDfk")]
public virtual ApplicationUser ApplicationUser { get; set; }
public Guid? UserIDfk { get; set; …Run Code Online (Sandbox Code Playgroud) 我正在努力理解代表,为此我为自己创建了一个简单的例子,但它不能按照我想要的方式工作,这是我的例子:
我的职责:
class Functions
{
public string MyFname(string MyString)
{
return MyString;
}
public string MyLname(string MyString)
{
return MyString;
}
}
Run Code Online (Sandbox Code Playgroud)
代表:
class DelClass
{
public delegate string StrDelegate(string OutPutString);
public void StringCon(StrDelegate Fname,StrDelegate Lname)
{
Console.WriteLine(Fname + " " + Lname);
}
}
Run Code Online (Sandbox Code Playgroud)
主要:
class Program
{
static void Main(string[] args)
{
DelClass MyDel = new DelClass();
Functions MyTster = new Functions();
DelClass.StrDelegate Name = new DelClass.StrDelegate(MyTster.MyFname);
Name("SomeVariableName");
DelClass.StrDelegate Family = new DelClass.StrDelegate(MyTster.MyLname);
Family("SomeVariableFamilyName");
MyDel.StringCon(Name, Family);
}
} …Run Code Online (Sandbox Code Playgroud)