سفارشی سازی ASP.NET Core Identity - قسمت اول - موجودیتهای پایه و DbContext برنامه
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۱۱/۱۰ ۱۱:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Role : IdentityRole<int, UserRole, RoleClaim>, IAuditableEntity
{
public Role()
{
}
public Role(string name)
: this()
{
Name = name;
}
public Role(string name, string description)
: this(name)
{
Description = description;
}
public string Description { get; set; }
}
public class UserRole : IdentityUserRole<int>, IAuditableEntity
{
public virtual User User { get; set; }
public virtual Role Role { get; set; }
}
public class RoleClaim : IdentityRoleClaim<int>, IAuditableEntity
{
public virtual Role Role { get; set; }
} public class UserClaim : IdentityUserClaim<int>, IAuditableEntity
{
public virtual User User { get; set; }
} public class UserToken : IdentityUserToken<int>, IAuditableEntity
{
public virtual User User { get; set; }
}
public class UserLogin : IdentityUserLogin<int>, IAuditableEntity
{
public virtual User User { get; set; }
} public class User : IdentityUser<int, UserClaim, UserRole, UserLogin>, IAuditableEntity
{
public User()
{
UserUsedPasswords = new HashSet<UserUsedPassword>();
UserTokens = new HashSet<UserToken>();
}
[StringLength(450)]
public string FirstName { get; set; }
[StringLength(450)]
public string LastName { get; set; }
[NotMapped]
public string DisplayName
{
get
{
var displayName = $"{FirstName} {LastName}";
return string.IsNullOrWhiteSpace(displayName) ? UserName : displayName;
}
}
[StringLength(450)]
public string PhotoFileName { get; set; }
public DateTimeOffset? BirthDate { get; set; }
public DateTimeOffset? CreatedDateTime { get; set; }
public DateTimeOffset? LastVisitDateTime { get; set; }
public bool IsEmailPublic { get; set; }
public string Location { set; get; }
public bool IsActive { get; set; } = true;
public virtual ICollection<UserUsedPassword> UserUsedPasswords { get; set; }
public virtual ICollection<UserToken> UserTokens { get; set; }
}
public class UserUsedPassword : IAuditableEntity
{
public int Id { get; set; }
public string HashedPassword { get; set; }
public virtual User User { get; set; }
public int UserId { get; set; }
}
public abstract class ApplicationDbContextBase : IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>, IUnitOfWork
protected void BeforeSaveTriggers()
{
ValidateEntities();
SetShadowProperties();
this.ApplyCorrectYeKe();
} public class ApplicationDbContext : ApplicationDbContextBase
protected override void OnModelCreating(ModelBuilder builder)
{
// it should be placed here, otherwise it will rewrite the following settings!
base.OnModelCreating(builder);
// Adds all of the ASP.NET Core Identity related mappings at once.
builder.AddCustomIdentityMappings(SiteSettings.Value);
// Custom application mappings
// This should be placed here, at the end.
builder.AddAuditableShadowProperties();
}
dotnet restore dotnet run
public static readonly Func<object, string> EFPropertyCreatedByIp =
entity => EF.Property<string>(entity, CreatedByIp);
public static readonly string CreatedByIp = nameof(CreatedByIp); public static readonly Func<object, DateTimeOffset?> EFPropertyCreatedDateTime =
entity => EF.Property<DateTimeOffset?>(entity, CreatedDateTime); var persons = context.Persons
.Where(x => AuditableShadowProperties.EFPropertyCreatedDateTime(x) == DateTimeOffset.UtcNow)
.ToList(); var items = context.Persons
.Where(x => EF.Property<DateTimeOffset?>(x, AuditableShadowProperties.CreatedDateTime) <= DateTimeOffset.UtcNow).ToList();
public class User : IdentityUser<int, UserClaim, UserRole, UserLogin>
public class ApplicationDbContext :
IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>, IUnitOfWork
{
// we can't use constructor injection anymore, because we are using the `AddDbContextPool<>`
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) { } System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)
public virtual User User { get; set; }
public virtual Role Role { get; set; } public virtual ICollection<UserRole> UserRoles { get; set; } = new HashSet<UserRole>();
Error CS0433 The type 'Image' exists in both 'CoreCompat.System.Drawing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c0a7ed9c2333b592' and 'System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
| .NET Core SDK | .NET Core Runtime | Compatible Visual Studio | MSBuild | Notes |
|---|---|---|---|---|
| 2.1.5nn | 2.1 | 2017 | 15 | Installed as part of VS 2017 version 15.9 |
| 2.1.6nn | 2.1 | 2019 | 16 | Installed as part of VS 2019 |
| 2.2.1nn | 2.2 | 2017 | 15 | Installed manually |
| 2.2.2nn | 2.2 | 2019 | 16 | Installed as part of VS 2019 |
| 3.0.1nn | 3.0 (Preview) | 2019 | 16 | Installed manually |
Visual Studio 2017 cannot work with .NET Core SDK 2.1.6nn or 2.2.2nn.
builder.AddDateTimeUtcKindConverter();
HTTP Error 500.30 - ANCM In-Process Start Failure
using System.Linq.Expressions;
public class ContextWithExtensionExample
{
public void DoSomeContextWork(DbContext context)
{
var uni = new Unicorn();
context.Set<Unicorn>().AddIfNotExists(uni , x => x.Name == "James");
}
}
public static class DbSetExtensions
{
public static T AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class, new()
{
var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any();
return !exists ? dbSet.Add(entity) : null;
}
} The configured execution strategy 'SqlServerRetryingExecutionStrategy' does not support user-initiated transactions. Use the execution strategy
returned by 'DbContext.Database.CreateExecutionStrategy()'
to execute all the operations in the transaction as a retriable unit.
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished HTTP/2 GET https://localhost:5001/ - 404 0 - 657.9667ms Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:5001/, Response status code: 404
dotnet dev-certs https --clean dotnet dev-certs https dotnet dev-certs https --trust dotnet dev-certs https --check --trust
dotnet clean dotnet build dotnet run