InvalidOperationException While Using the Existing DB in MVC Core - asp.net-core-mvc

I tried to use the existing database in my application but every time I hit the view it says
An unhandled exception occurred while processing the
InvalidOperationException: Unable to resolve service for type
'BookStore.Models.BookStoreContext' while attempting to activate
'BookStore.Models.UsersRepo'
Context
namespace BookStore.Models
{
public partial class BookStoreContext : DbContext
{
public BookStoreContext()
{
}
public BookStoreContext(DbContextOptions<BookStoreContext> options)
: base(options)
{
}
public virtual DbSet<Users> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\V11.0;Database=BookStore;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.3-servicing-35854");
modelBuilder.Entity<Users>(entity =>
{
entity.HasKey(e => e.UserId);
entity.Property(e => e.UserId).HasColumnName("User_ID");
entity.Property(e => e.Password)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.UserName)
.IsRequired()
.HasColumnName("User_Name")
.HasMaxLength(50);
});
}
}
}
User Repository
namespace BookStore.Models
{
public class UsersRepo : IUser
{
private readonly BookStoreContext _bookStoreContext;
public UsersRepo(BookStoreContext bookStoreContext)
{
_bookStoreContext = bookStoreContext;
}
public void AddUser(Users users)
{
_bookStoreContext.Users.Add(users);
_bookStoreContext.SaveChanges();
}
}
}
User Model
public partial class Users
{
public long UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int Type { get; set; }
}
public interface IUser
{
void AddUser(Users users);
}
User Controller
public class UsersController : Controller
{
private readonly IUser _userRepo;
public UsersController(IUser userRepo)
{
_userRepo = userRepo;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Users users)
{
_userRepo.AddUser(users);
return RedirectToAction("UserAddedSuccessfully");
}
public IActionResult UserAddedSuccessfully()
{
return View();
}
}

I tried this one and it is perfectly working
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<BookStoreContext>();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}

Related

Asp.Net Web api: view all logged in users

I tried to implement the function without success
I want to check if the user's status is true so that means he is logged in
I mean if Status is false then I do not want to display it at all in a list that will be ignored
What I tried causes all the connected to be displayed even if they are false
Model:
[Table("Contact")]
public partial class Contact
{
[Key]
public Guid Id { get; set; }
public string DisplayName { get; set; } = null!;
public string ProfilePic { get; set; } = null!;
public int? Rating { get; set; }
public bool? Status { get; set; }
public bool? IsRegistration { get; set; }
}
DbContext:
public virtual DbSet<Contact> Contacts { get; set; } = null!;
Repository:
private readonly TalkBackContactsDbContext _context;
public ContactsRepository(TalkBackContactsDbContext context)
{
_context = context;
}
public IQueryable<Contact> GetAllConnectedUser(Contact contact)
{
if (contact.Status == false)
{
throw new Exception("Not exist");
}
else
{
return _context.Contacts;
}
}
api controller:
private readonly IContactsRepository _repo;
public ContactsController(IContactsRepository repo)
{
_repo = repo;
}
[HttpGet()]
public IEnumerable<Contact> GetAllConnected()
{
var contact = new Contact();
try
{
return _repo.GetAllConnectedUser(contact);
}
catch (Exception e)
{
e.ToString();
}
return _repo.GetAllConnectedUser(contact);
}
Try this.
Repository:
private readonly TalkBackContactsDbContext _context;
public ContactsRepository(TalkBackContactsDbContext context)
{
_context = context;
}
public IEnumerable<Contact> GetAllConnectedUser()
{
_context.Contacts.Where(a=>a.Status==true).AsEnumerable();
}
api controller:
private readonly IContactsRepository _repo;
public ContactsController(IContactsRepository repo)
{
_repo = repo;
}
[HttpGet()]
public IEnumerable<Contact> GetAllConnected()
{
return _repo.GetAllConnectedUser();
}

ASP API CORE : Get relation Many to One

I have two entities : Article and PrixVariation.
One Article has Many Prix variation.
One Prix variation has One Article.
public partial class Article
{
public Article()
{
PrixVariations = new HashSet<PrixVariation>();
}
public int Id { get; set; }
public DateTime DateTime { get; set; }
...
public virtual ICollection<PrixVariation> PrixVariations { get; set; }
}
public partial class PrixVariation
{
public int Id { get; set; }
public DateTime DateTime { get; set; }
public int Article { get; set; }
public double Prix { get; set; }
public virtual Article ArticleNavigation { get; set; } = null!;
}
My Context is as follow :
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Article>(entity =>
{
entity.ToTable("articles");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.DateTime)
.HasColumnType("datetime")
.HasColumnName("dateTime")
.HasDefaultValueSql("(getdate())");
});
modelBuilder.Entity<PrixVariation>(entity =>
{
entity.ToTable("prix_variation");
entity.Property(e => e.Id)
.ValueGeneratedNever()
.HasColumnName("id");
entity.Property(e => e.Article).HasColumnName("article");
entity.Property(e => e.DateTime)
.HasColumnType("datetime")
.HasColumnName("dateTime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.Prix).HasColumnName("prix");
entity.HasOne(d => d.ArticleNavigation)
.WithMany(p => p.PrixVariations)
.HasForeignKey(d => d.Article)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_prix_variation_articles");
});
And my controller is as follow :
public class ArticlesController : ControllerBase
{
private readonly STGEORGESContext _context;
public ArticlesController(STGEORGESContext context)
{
_context = context;
}
// GET: api/Articles
[HttpGet]
public async Task<ActionResult<IEnumerable<Article>>> GetArticles()
{
return await _context.Articles.ToListAsync();
}
....
There is something not working here because when I launch the debogger, the collection of PrixVaration is always empty :
{"id":1,"dateTime":"2021-11-28T08:37:17","prixVariations":[]}
And of course in the database there is one PrixVaration linked to that Article..
Can anyone can help ?? Thaks a lot !!!
Pierre
It is called lazy loading. Ef doesn't load any object collections if you don't ask about it explicitly. So try this
return await _context.Articles.Include(i=> i.PrixVariations).ToListAsync();

odata v4, List<> Property without expand

Is it possible in odata4 to create a model such as:
public class PuppyDogs
{
public string Name { get; set; }
public virtual IList<Bone> Bones { get; set; }
}
public class Bone
{
public string ChewType { get; set; }
public int Numberofchews { get; set; }
}
And the controller class looks like
public class PuppyDogController : ODataController
{
List<PuppysDog> mydogs = new List<PuppysDog>();
private PuppyDogController()
{
if (mydogs.Count == 0)
{
PuppysDog mydog = new PuppysDog();
mydog.Name = "Fido";
mydog.Bones = new List<Bone>()
{
new Bone{ ChewType = "Soft", Numberofchews=1 },
new Bone{ ChewType = "Hard", Numberofchews=2 }
};
mydogs.Add(mydog);
}
}
[EnableQuery]
public IQueryable<PuppysDog> Get()
{
return mydogs.AsQueryable();
}
}
Can I include the Bones property of PuppyDogs without using expand? By default Bones is not returned to the client.
There are several things nor clear in your code, for example, the entity set PuppyDogs don't have a key, the naming convention in the controller is a little wired and etc. With the following code, it can work perfectly, please take a look
PuppyDog.cs
public class PuppyDog
{
[Key]
public string Name { get; set; }
public virtual IList<Bone> Bones { get; set; }
}
Bone.cs
public class Bone
{
public string ChewType { get; set; }
public int Numberofchews { get; set; }
}
PupyyDogsController.cs
public class PuppyDogsController : ODataController
{
List<PuppyDog> mydogs = new List<PuppyDog>();
private PuppyDogsController()
{
if (mydogs.Count == 0)
{
PuppyDog mydog = new PuppyDog();
mydog.Name = "Fido";
mydog.Bones = new List<Bone>()
{
new Bone {ChewType = "Soft", Numberofchews = 1},
new Bone {ChewType = "Hard", Numberofchews = 2}
};
mydogs.Add(mydog);
}
}
[EnableQuery]
public IQueryable<PuppyDog> Get()
{
return mydogs.AsQueryable();
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<PuppyDog>("PuppyDogs");
config.MapODataServiceRoute("odata", null, builder.GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}
}
Then when try http://localhost:21830/PuppyDogs, I can successfully got the payload as
{
"#odata.context": "http://localhost:21830/$metadata#PuppyDogs",
"value": [
{
"Name": "Fido",
"Bones": [
{
"ChewType": "Soft",
"Numberofchews": 1
},
{
"ChewType": "Hard",
"Numberofchews": 2
}
]
}
]
}

entity framework programming against interface and repository pattern

I am developing in ASP.NET MVC3 and EntityFramework.
I want my model to follow an interface :
public class Account : IAccount
{
public string Id { get; set; }
public DateTime Date { get; set; }
public string Language { get; set; }
}
public interface IAccount
{
string Id { get; set; }
DateTime Date { get; set; }
string Language { get; set; }
}
Here's my Context
public class EFContext : DbContext, IContext
{
public DbSet<Account> Accounts { get; set; }
}
And here's the repository :
public interface IRepository<T> where T : class
{
IQueryable<T> All { get; }
int Count { get; }
bool Contains(Expression<Func<T, bool>> predicate);
void Create(T item);
void Update(T item);
void Delete(Expression<Func<T, bool>> predicate);
void Delete(T item);
}
public class EFRepository<T> : IRepository<T> where T : class
{
private EFContext _context;
public EFRepository(IUnitOfWork uow)
{
this._context = (EFContext)uow.Context;
}
protected DbSet<T> DbSet
{
get
{
return _context.Set<T>();
}
}
public IQueryable<T> All
{
get
{
return DbSet.AsQueryable();
}
}
public virtual int Count
{
get
{
return DbSet.Count();
}
}
public bool Contains(Expression<Func<T, bool>> predicate)
{
return DbSet.Count(predicate) > 0;
}
public virtual void Create(T item)
{
DbSet.Add(item);
}
public virtual void Update(T TObject)
{
var item = DbSet.Attach(TObject);
_context.SetItemState(TObject, EntityState.Modified);
}
public virtual void Delete(Expression<Func<T, bool>> predicate)
{
var objects = DbSet.Where(predicate);
foreach (var obj in objects)
{
DbSet.Remove(obj);
}
}
public virtual void Delete(T TObject)
{
DbSet.Remove(TObject);
}
}
Now, I want to use IRepository<IAccount> but this will ask the context for DbSet<IAccount>. This leads to an error since the Context contains a DbSet<Account>.
I then tried the solution proposed here for Linq2Sql : http://iridescence.no/post/Linq-to-Sql-Programming-Against-an-Interface-and-the-Repository-Pattern.aspx
So I added this function to my EFContext
public new DbSet<T> Set<T>() where T : class
{
var ciccio = TableMaps[typeof(T)];
return (DbSet<T>)base.Set(ciccio).Cast<T>();
}
But it doesn't work.
Do anyone have a suggestion?
Thx
What benefit are you receiving from using an interface for your entities? I don't see any value here. Typically, you use Interfaces to remove dependencies upon the implementation, but that's not what you're achieving here because you're returning a concrete DbSet of objects.
Your entities are already Poco's. They don't have dependencies on other implemntations, and they have no code in them other than a getter/setter. Using an interface is redundant and pointless.
I've found a workaround. I kind of like it so I want to share it.
I rewritten my EFRepository :
public class EFRepository<T, W> :
IRepository<T> where T : class
where W : class, T
{
private EFContext _context;
public EFRepository(IUnitOfWork uow)
{
this._context = (EFContext)uow.Context;
}
protected DbSet<W> DbSet
{
get
{
return _context.Set<W>();
}
}
public IQueryable<T> All
{
get
{
return DbSet.AsQueryable<T>();
}
}
public virtual int Count
{
get
{
return DbSet.Count();
}
}
public bool Contains(Expression<Func<T, bool>> predicate)
{
return All.Count(predicate) > 0;
}
public virtual void Create(T item)
{
DbSet.Add(item as W);
}
public virtual void Update(T TObject)
{
var item = DbSet.Attach(TObject as W);
_context.SetItemState(TObject, EntityState.Modified);
}
public virtual void Delete(Expression<Func<T, bool>> predicate)
{
var objects = All.Where(predicate);
foreach (var obj in objects)
{
DbSet.Remove(obj as W);
}
}
public virtual void Delete(T TObject)
{
DbSet.Remove(TObject as W);
}
}
So now basically all I need to do now is
IRepository<IAccount>> accRepository = new EFRepository<IAccount, Account>(uow);
I am happy with this solution, but still I'm not sure it is the best one, so any comments will be appreciated.
Thanks

Where putting logic to Approval user?

In my application got:
Classes
public class User
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public bool IsApproved { get; set; }
}
public class DataContext : DbContext
{
DbSet<User> Users { get; set; }
}
public class Repository
{
DataContext db = new DataContext();
public bool ApproveUser(User usr) //This is correct place?
{
usr.IsApproved = true;
db.Attrach(usr);
return db.SaveChanges() > 0;
}
}
Question
Where putting logic approval user?
In Repository? In own class?
I ask this because today is the repository and am having trouble to test this once approval is the logic of production in the repository in the repository and not fake.
Repository is the place to write data access. User approval is more likely to be business process, so it better be separated from data access. I would do it this way (code below is more like of pseudocode, not the full production-ready stuff)
public interface IUserRepository
{
bool Save();
}
public class UserRepository : IUserRepository
{
public bool Save(User user)
{
db.Attrach(user);
return db.SaveChanges() > 0;
}
}
public interface IUserService
{
bool Approve(User user);
}
public class UserService : IUserService
{
readonly IUserRepository _userRepository;
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public bool Approve(User user)
{
user.IsApproved = true;
return _repository.Save(User user);
}
}
And now, this already is the testable code

Resources