Add-Migration Error: Object reference not set to an instance of an object. Asp.Net - migration

[enter image description here][1]
[1]: https://i.stack.imgur.com/QNwMk.png`enter code here`
Migration did not occur if I did not add all packages.I'm new to this job. I would be glad if you help.
PhoneBookContext
namespace PhoneBook.Repository.Concrate.EntityFramework
{
public class PhoneBookContext:DbContext
{
public PhoneBookContext(DbContextOptions<PhoneBookContext>options):base(options)
{
}
public DbSet<Persons> Persons { get; set; }
public DbSet<Informations> Informations { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonsInformation>()
.HasKey(pk => new { pk.PersonID, pk.InformationsID });
}
}
}
EfPersonsRepository
namespace PhoneBook.Repository.Concrate.EntityFramework
{
public class EfPersonsRepository : IPersonsRepository
{
private PhoneBookContext context;
public EfPersonsRepository(PhoneBookContext ctx)
{
context = ctx;
}
public IQueryable<Persons> Persons => context.Persons;
}
}

Related

system.outofmemoryexception swashbuckle.aspnetcore

I am having this issue when I am dealing with Geometry datatypes when I change the property to string everything works like a charm. Below you may see that I used schema filter to remove Ignored data member , and document filter to remove anything related to nettopology.
Property Name = GeoPoly
Swagger Config Class
public static IServiceCollection AddSwaggerModule(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v2", new OpenApiInfo { Title = "Test API", Version = "0.0.1" });
c.SchemaFilter<MySwaggerSchemaFilter>();
c.DocumentFilter<RemoveBogusDefinitionsDocumentFilter>();
c.ResolveConflictingActions(x => x.First());
});
return services;
}
public static IApplicationBuilder UseApplicationSwagger(this IApplicationBuilder app)
{
app.UseSwagger(c =>
{
c.RouteTemplate = "{documentName}/api-docs";
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/v2/api-docs", "Test API");
});
return app;
}
}
public class MySwaggerSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null)
{
return;
}
var ignoreDataMemberProperties = context.Type.GetProperties()
.Where(t => t.GetCustomAttribute<IgnoreDataMemberAttribute>() != null);
foreach (var ignoreDataMemberProperty in ignoreDataMemberProperties)
{
var propertyToHide = schema.Properties.Keys
.SingleOrDefault(x => x.ToLower() == ignoreDataMemberProperty.Name.ToLower());
if (propertyToHide != null)
{
schema.Properties.Remove(propertyToHide);
}
}
}
}
public class RemoveBogusDefinitionsDocumentFilter : Swashbuckle.AspNetCore.SwaggerGen.IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Components.Schemas.Remove("Districts");
swaggerDoc.Components.Schemas.Remove("Geometry");
swaggerDoc.Components.Schemas.Remove("CoordinateSequenceFactory");
swaggerDoc.Components.Schemas.Remove("GeometryOverlay");
swaggerDoc.Components.Schemas.Remove("NtsGeometryServices");
swaggerDoc.Components.Schemas.Remove("CoordinateEqualityComparer");
swaggerDoc.Components.Schemas.Remove("NtsGeometryServices");
swaggerDoc.Components.Schemas.Remove("GeometryFactory");
swaggerDoc.Components.Schemas.Remove("OgcGeometryType");
swaggerDoc.Components.Schemas.Remove("Coordinate");
swaggerDoc.Components.Schemas.Remove("Point");
}
}
Entity Class
public class Districts : BaseEntity<long>
{
public string DistrictsDesc { get; set; }
public string DistrictsDescAr { get; set; }
[IgnoreDataMember]
[Column(TypeName = "geometry")]
public Geometry GeoPoly { get; set; }
public IList<Records> Records { get; set; } = new List<Records>();
public long? RegionsId { get; set; }
public Regions Regions { get; set; }
public long? CitiesId { get; set; }
public Cities Cities { get; set; }
}
Is there a way to stop swashbuckle gen from dealing with datatypes other than documents filter ?

.net Core 3 / API / DI / Repository pattern

I'm trying to create a web API in .net Core. I would like this API calls a DLL with repository design pattern implemented in it. In this whole thing I tried to use dependency injection but I have some issues to manage context for database in repository.
I would like than the context one new context per lifetime of a call to the API.
When I try to execute my code I have an exception at line CreateHostBuilder(args).Build().Run(); in Main.
This Exception is :
'Some services are not able to be constructed'
InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.
I know the code is incomplete and won't work completely but at least it should break way later than actually. I think I made a mistake in the architecture.
There is a lot of code below but I couldn't isolate my problem.
Thank you everybody.
In my API I have :
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IUserService, UserService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
User.cs
public class User
{
public User() {}
public int UserId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime Birthdate { get; set; }
}
UserService.cs
public class UserService : IUserService
{
private readonly AppSettings _appSettings;
private readonly IUsersRepository _userRepository;
public UserService(IOptions<AppSettings> appSettings, IUsersRepository userRepository)
{
_appSettings = appSettings.Value;
_userRepository = userRepository;
}
public IEnumerable<User> GetAll()
{
return _userRepository.GetAllUsers().Select(u=> new User());
}
}
IUserService.cs
public interface IUserService
{
public IEnumerable<User> GetAll();
}
AppSettings.cs
public class AppSettings
{
public string Secret { get; set; }
}
UsersController.cs
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private IUserService _userService { get; }
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet]
public IActionResult GetAll()
{
var users = _userService.GetAll();
return Ok(users);
}
}
In the DLL I have :
SecurityContext.cs
public partial class SecurityContext : DbContext
{
public SecurityContext(DbContextOptions<SecurityContext> options) : base(options) { }
public DbSet<Users> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=; Database=BXDB; User Id=sa; Password=;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>(entity =>
{
entity.HasKey(e => e.UserId).HasName("PK_User_UserId");
entity.ToTable("Users", "sec");
entity.Property(e => e.Birthdate).HasColumnType("date");
entity.Property(e => e.FirstName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(50)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Users.cs
public class Users
{
public Users() { }
public int UserId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime Birthdate { get; set; }
}
IUsersRepository.cs
public interface IUsersRepository
{
public IQueryable<Users> GetAllUsers();
}
UsersRepository.cs
public class UsersRepository : IUsersRepository
{
public readonly SecurityContext _dbContext;
public UsersRepository(SecurityContext dbContext)
{
_dbContext = dbContext;
}
public IQueryable<Users> GetAllUsers()
{
return _dbContext.Users;
}
}
'Some services are not able to be constructed' InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.
From the error , you should register the DbContext as a service as follows:
public void ConfigureServices(IServiceCollection services)
{
var connection = #"Server=(localdb)\mssqllocaldb;Database=BXDB;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<DLL.Models.SecurityContext>(options => options.UseSqlServer(connection, x => x.MigrationsAssembly("DLL")));
services.AddControllers();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IUserService, UserService>();
}

Invalid column name 'EmailAddress' when using generic repository, but works fine with context

Getting the mentioned error when trying to do a GetAll on accounts. It works fine if I go directly to the dbcontext, but gives me the error if I try to work with the repo. I have about 20 others that use just the generic repo and are working great. Because I have additional actions for Accounts, I have created its own repository that implements the generic. I also have several others that work like this and have no problem. The problem is specific to the accounts.
Database of course does have the EmailAddress column, since I can return it if I use dbcontext from the controller instead of the repo.
Any help would be much appreciated.
AccountsController:
public class AccountsController : ControllerBase
{
private readonly AccountRepository _repo;
public AccountsController(DatabaseContext context)
{
_repo = new AccountRepository(context);
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Account>>> GetAccount()
{
// return _context.Account.ToListAsync(); works fine if _context is defined
var accounts = await _repo.GetAll();
if (accounts == null)
return NoContent();
return Ok(accounts); // Gives invalid column error
}
[HttpGet("getaccount")]
public async Task<ActionResult<Account>> GetCurrentAccount()
{
var account = await _repo.GetCurrentAccount(HttpContext.User.Identity.Name);
if (account == null)
{
return NotFound();
}
return account; // Works fine
}
}
Account:
public partial class Account
{
public string Name { get; set; }
public string RefId { get; set; }
public string Position { get; set; }
public bool IsActive { get; set; }
public string EmailAddress { get; set; }
[Key]
public string UserId { get; set; }
}
IAccountRepository:
public interface IAccountRepository : IRepository<Account>
{
Task<Account> GetCurrentAccount(string emailAddress);
}
AccountRepository:
public class AccountRepository : Repository<Account>, IAccountRepository
{
private DatabaseContext _context;
public AccountRepository(DatabaseContext context)
{
_context = context;
}
public async Task<Account> GetCurrentAccount(string emailAddress)
{
var account = await _context.Account
.Where(a => a.EmailAddress == emailAddress)
.FirstOrDefaultAsync();
return account; // this works just fine, and returns with EmailAddress
}
}
IRepository (generic):
public interface IRepository<T>
{
Task<IEnumerable<T>> GetAll();
Task<T> GetById(object id);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
Task<bool> Save();
}
Repository (generic):
public class Repository<T> : IRepository<T> where T : class
{
private DatabaseContext _context;
public Repository()
{
_context = new DatabaseContext();
}
public Repository(DatabaseContext context)
{
_context = context;
}
public void Add(T obj)
{
_context.Set<T>().Add(obj);
}
public void Delete(T entity)
{
_context.Set<T>().Remove(entity);
}
public async Task<IEnumerable<T>> GetAll()
{
return await _context.Set<T>().ToListAsync();
}
public async Task<T> GetById(object id)
{
return await _context.Set<T>().FindAsync(id);
}
public void Update(T obj)
{
_context.Set<T>().Update(obj);
}
public async Task<bool> Save()
{
try
{
await _context.SaveChangesAsync();
}
catch (Exception)
{
return false;
}
return true;
}
}
EDIT
I should mention that EmailAddress was added to the database via EF migration.

How to use FluentValidation.AspNetCore and FluentValidation.MVC6?

How to use FluentValidation.AspNetCore and FluentValidation.MVC6 to validate Entities in AspNetCore , can anyone give me an example ?
This is working for me:
project.json add:
"FluentValidation.AspNetCore": "6.4.0-beta3"
startup.cs
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
Validation:
public class Foo
{
public string Bar {get; set;}
}
public class FooValidator : AbstractValidator<Foo>
{
public FooValidator()
{
RuleFor(x => x.Bar).NotEmpty().WithMessage("Error Message");
}
}
first you need to add nuget Install-Package FluentValidation.AspNetCore
you can have an action filter which will handle validation:
public class ValidatorActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.ModelState.IsValid)
{
filterContext.Result = new BadRequestObjectResult(filterContext.ModelState);
}
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
}
Startup.ConfigureServices looks like this to add auto validation and add FluentValidation:
services.AddMvc(opt =>
{
opt.Filters.Add(typeof(ValidatorActionFilter));
}).AddFluentValidation(fvc =>
fvc.RegisterValidatorsFromAssemblyContaining<Startup>())
If you need to read another assembly, name one of its classes instead of startUp
Now you can add a validation to ensure:
public class CreateCustomer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public class CreateCustomerValidator : AbstractValidator<CreateCustomer>
{
public CreateCustomerValidator()
{
RuleFor(x => x.FirstName).NotNull().WithMessage(Resource.Validaton.FirstNameRequired);
RuleFor(x => x.LastName).NotNull().WithMessage(Resource.Validaton.LastNameRequired);
RuleFor(x => x.Email).Matches(#"\A(?:[a-z0-9!#$%&'*=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z").WithMessage(Resource.Validaton.EmailFormat);
}
}

RavenDB UniqueConstraint doesn't seem to work

I've been trying for a day to get UniqueConstraint working, but it doesn't seem the are. I have a simple MVC6 site that creates a User on a POST. I'm expecting that on the second POST an exception should be thrown as a user will have already been created with the same properties. I'm wanting to ensure that the email address is unique.
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.UniqueConstraints;
namespace MVC6Test.DomainModel
{
public class User
{
public string Id { get; private set; }
[UniqueConstraint]
public string Email { get; set; }
public string Password { get; set; }
public string Name { get; set; }
}
}
namespace MVC6Test.Web.Controllers
{
public class AdminController : Microsoft.AspNet.Mvc.Controller
{
private IDocumentStore _documentStore { get; set; }
public IDocumentSession Session { get; set; }
[HttpPost]
[AllowAnonymous]
[Route("login")]
public async Task<IActionResult> Login(string userName, string password)
{
User user = new User() {
Email = "test#gmail.com"
};
Session.Store(user);
}
public override void OnActionExecuting(ActionExecutingContext context)
{
if (_documentStore.IsDefault()) {
_documentStore = context.HttpContext.RequestServices.GetRequiredService<IDocumentStore>();
}
Session = _documentStore.OpenSession();
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
using (Session) {
if (Session != null && context.Exception == null) {
Session.SaveChanges();
}
}
base.OnActionExecuted(context);
}
}
}
namespace MVC6Test.Web
{
public class Startup
{
private IDocumentStore DocumentStore;
public void ConfigureServices(IServiceCollection services)
{
DocumentStore = new DocumentStore {
DefaultDatabase = "MVC6Test",
Url = "http://localhost:3366"
};
DocumentStore.Listeners.RegisterListener(new UniqueConstraintsStoreListener());
DocumentStore.Initialize();
services.TryAddSingleton(typeof(IDocumentStore), (provider) => {
return DocumentStore;
});
}
public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime)
{
lifetime.ApplicationStopped.Register(() => {
DocumentStore.Dispose();
});
}
}
}
I do get this metadata on the items that are created:
{
"Raven-Entity-Name": "Users",
"Raven-Clr-Type": "MVC6Test.DomainModel.User, MVC6Test",
"Ensure-Unique-Constraints": [
{
"Name": "Email",
"CaseInsensitive": false
}
]
}