How to test if my linq query went through - c#

This is my code from my controller:
MGEntities db = new MGEntities();
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
MembershipUser myObject = Membership.GetUser();
Guid UserID = (Guid)myObject.ProviderUserKey;
MyProfile profile = new MyProfile();
profile.Address = model.Address;
profile.City = model.City;
profile.Zip = model.Zip;
profile.State = model.State;
profile.UserId = UserID;
Debug.Write(profile.State);
db.aspnet_Profiles.Add(profile);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
This is my MyProfile Class:
namespace MatchGaming.Models
{
[Bind(Exclude = "ProfileId")]
public class MyProfile
{
[Key]
[ScaffoldColumn(false)]
public int ProfileId { get; set; }
public Guid UserId { get; set; }
[DisplayName("Address")]
public string Address { get; set; }
[DisplayName("City")]
public string City { get; set; }
[DisplayName("Zip")]
public string Zip { get; set; }
[DisplayName("State")]
public string State { get; set; }
}
}
After the linq query is executed, i check my database and nothing is added. I am using POCO for my entities. Here is my class:
namespace MatchGaming.Models
{
public class MGEntities : DbContext
{
public DbSet<MyProfile> aspnet_Profiles { get; set; }
}
}
I basically just dont understand why its not adding to the database, if theres a way I can check if the query went through correctly or not or if anyone can see the problem. Thank you!

Try
db.aspnet_Profiles.Add(profile);
db.SaveChanges();

You're misusing EF.
EF contexts are not thread safe and cannot be reused across requests.
You need to create a separate context (MGEntities) for each request, by creating it in the controller in a using statement.
You also need to call SaveChanges().

Related

Bind new modal field in asp.net core modal

My code was working file until i added a new field in modal which has only get method
public bool hasShiftingRequest {
//this field is not in database
//it is being calculated everytime you access it
get
{
return _context.AssetShifting.Where(a => a.assetId == this.Id & a.status.Equals("REQUESTED")).Any();
}
}
But it is causing error during my edit method which is binding fronted data with modal
(Basically problem during Binding)
public async Task<IActionResult> Edit(int id, [Bind("Id,make_model,lot,username,email")] AssetDataPc assetDataPc)
and I am getting this error
Please Help !
EDIT
My assetPC modal
public class AssetDataPc
{
public readonly AssetManagementContext _context;
public AssetDataPc(AssetManagementContext context)
{
_context = context;
}
public int ram { get; set; }
[Display(Name = "Remarks")]
public string remarks { get; set; }
[Display(Name = "QR Code Status")]
public string qr_code_status { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[Display(Name = "Last updated")]
public DateTime updated_at { get; set; } = DateTime.Now;
[EmailAddress]
[Display(Name = "Email")]
public string email { get; set; }
[Display(Name = "Screen Size")]
public string screen_size { get; set; }
[Display(Name = "Color")]
public string rowColor { get; set; } = "";
public bool hasShiftingRequest {
//this field is not in database
//it is being calculated everytime you access it
get
{
return _context.AssetShifting.Where(a => a.assetId == this.Id & a.status.Equals("REQUESTED")).Any();
}
}
}
EDIT 2
my edit (POST( method is some what like this
public async Task<IActionResult> Edit(int id, [Bind("remarks,qr_code_status,email")] AssetDataPc assetDataPc)
{
if (ModelState.IsValid)
{
assetDataPc.updated_at = DateTime.Now;
_context.Update(assetDataPc);
await _context.SaveChangesAsync();
}
EDIT 3
My edit (Get) method:
public async Task<IActionResult> Edit(int? id)
{
var assetDataPc = await _context.AssetDataPcs.FindAsync(id);
if (assetDataPc == null)
{
return NotFound();
}
return View(assetDataPc);
}
hasShiftingRequest is not in your database?
Then use [NotMapped] if you need to use extra column without adding this column in database so that entity framework core will not check this matching column between model class and table in database.
[NotMapped]
public bool? hasShiftingRequest { get; set; }
Remove AssetManagementContext from your AssetDataPc model. Like this.
public class AssetDataPc
{
[NotMapped]
public bool? hasShiftingRequest { get; set; }
}
"Get" Edit method
public async Task<IActionResult> Edit(int? id)
{
var assetDataPc = await _context.AssetDataPcs.FindAsync(id);
if (assetDataPc == null)
{
return NotFound();
}
else
assetDataPc.hasShiftingRequest = _context.AssetShifting.Where(a => a.assetId == assetDataPc.Id & a.status.Equals("REQUESTED")).Any();
return View(assetDataPc);
}
Solution
--dont use DbContext in modal classes
--use [NotMapped] to avoid creating database field
As the exception states AssetDataPc should have a parameterless constructor in order to be binded. When you added this constructor
public AssetDataPc(AssetManagementContext context)
{
_context = context;
}
it started failing.
Consider moving hasShiftingRequest logic outside the class and just map result to plain property.
As the error message said, Model bound complex types must not be abstract or value types and must have a parameterless constructor. So, you could try to add the default AssetDataPc constructor for the AssetDataPc class.
public class AssetDataPc
{
public readonly AssetManagementContext _context;
public AssetDataPc(){} //add default constructor
public AssetDataPc(AssetManagementContext context)
{
_context = context;
}
...
public bool hasShiftingRequest {
//this field is not in database
//it is being calculated everytime you access it
get
{
return _context.AssetShifting.Where(a => a.assetId == this.Id & a.status.Equals("REQUESTED")).Any();
}
}
}

fetch data from the given id in the controller asp.net mvc

I need to fetch a string from the database which is saved to it when adding a business in my case. I was able to save it to the db via below code in business controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,Address,Category,Description,Latitude,Longitute,Owner")] Business business)
{
business.Owner = System.Web.HttpContext.Current.User.Identity.Name;//I'm saving the current user as the owner
if (ModelState.IsValid)
{
db.Businesses.Add(business);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(business);
}
Now all I need is to check whether the current user user is the owner of the business which is saved in the model when adding a business as seen in the above code. the model class is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebPortal.Models
{
public class Business
{
public int ID { get; set; }
public string Title { get; set; }
public string Address { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Owner { get; set; }//in order to detect the original creator of the businness
}
}
The saving part works fine and here's is the code which i tried to fetch it in the business controller
// GET: Businesses/Edit/5
[Authorize]
public ActionResult Edit([Bind(Include = "ID,Title,Address,Category,Description,Latitude,Longitute,Owner")] int? id, string ownerr, Business business)
{
Business bs = new Business();
//Authorizing Edit permission only for the owner of the business and the admin
if (!((System.Web.HttpContext.Current.User.Identity.Name == bs.Owner
|| User.Identity.Name == "admin#gmail.com" )))
{
return View(db.Businesses.ToList());
}
It's kinda wronge. i just need to know how the fetch the relavent owner of the business by passing the ID maybe...
Edited
Id could be get via below html and i was trying to pass the owner as well but it returns a null in the controller
{
#Html.ActionLink("| Edit | ", "Edit", new { id = item.ID, owner = item.Owner })
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
}
I usually make Identity Id the Id of the models and I use the ApplicationUserManager to query the database for the currently Signed In user
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
var user = UserManager.FindById(User.Identity.GetUserId());
var userId = Guid.Parse(user.Id);
var _context = new MessageContext();
var myContacts = _context.Contacts.Where(c => c.CustomerId == userId).ToList();
ViewBag.Contacts = myContacts;

Unable to get Namespace from ObjectStateEntry when saving from a ViewController

I'm attempting to create an Audit Log for my MVC, Entity Framework website project. I've been able to subscribe to SaveChanges() in my DBContext (and save to my database through another DBContext but same database).
My two questions in the end are:
What does if (!entry.IsRelationship) do exactly? I have a ViewModel that calculates this as True when Saving and another as False. I would expect this to move into the rest of my method to save in the Audit Log.
How can I get the full Namespace of my Object being modified? I was using this: entry.Entity.ToString() but doesn't seem to work when Saving/Editing from a View Model (details below)
Here is a basic setup that I have thus far (Album object/controller works, but AlbumView doesn't):
Ablum class:
public class Album : BaseObject //BaseObject has a few properties, one is Oid (Guid)
{
public string Name { get; set; }
[Column(TypeName = "varchar(MAX)")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Display(Name="Genres")]
public virtual ICollection<AlbumsGenres> AlbumGenres { get; set; }
[Display(Name="Artists")]
public virtual ICollection<AlbumsArtists> AlbumArtists { get; set; }
}
AblumView class:
public class AlbumView
{
[ScaffoldColumn(false)]
public Guid Oid { get; set; }
[Required]
public string Name { get; set; }
[Column(TypeName = "varchar(MAX)")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Display(Name = "Genres")]
public virtual List<AlbumsGenres> AlbumGenres { get; set; }
[Display(Name = "Artists")]
public virtual List<AlbumsArtists> AlbumArtists { get; set; }
}
AlbumsController (Audit works with something like this):
public ActionResult Edit(Album album)
{
if (ModelState.IsValid)
{
db.Entry(album).State = EntityState.Modified;
db.SaveChanges(); //This is where SaveChanges() takes over (see below)
return RedirectToAction("Index");
}
return View(album);
}
AlbumsViewController:
public ActionResult Edit(Guid id, AlbumView albumViewModel)
{
//Omitting setup...
//Album gets updated
Album album = db.Albums.Find(id);
album.Name = albumViewModel.Name;
album.Description = albumViewModel.Description;
//Other Objects are also updated, just an example:
albumArtists = new AlbumsArtists();
albumArtists.Oid = Guid.NewGuid();
albumArtists.Album = db.Albums.Find(id);
albumArtists.Artist = db.Artists.Find(item.Artist.Oid);
//In the end it calls:
db.SaveChanges();
//Omitting other stuff...
}
On db.SaveChanges() within my DbContext:
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext() : base("name=DefaultConnection") { }
public System.Data.Entity.DbSet<ContentPub.Models.Music.Album> Albums { get; set; }
//Other DBSet objects...
public DbSet Set(string name)
{
return base.Set(Type.GetType(name));
}
public override int SaveChanges()
{
ApplicationLogDBContext logDb = new ApplicationLogDBContext();
ChangeTracker.DetectChanges();
ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;
List<ObjectStateEntry> objectStateEntryList =
ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
| EntityState.Modified
| EntityState.Deleted)
.ToList();
foreach (ObjectStateEntry entry in objectStateEntryList)
{
Guid oid = Guid.Empty;
try
{
if (!entry.IsRelationship) //I don't understand this (first of my two questions)
{
switch (entry.State)
{
//Removed other cases
case EntityState.Modified:
{
oid = (Guid)entry.EntityKey.EntityKeyValues[0].Value;
//This is the area that I am having issues (second of the two questions)
//Below will work when I call db.SaveChanges() from the AlbumsController,
//'entry.Entity.ToString()' will get 'x.Models.Music.Albums' and begin a query
var query = this.Set(entry.Entity.ToString()).AsNoTracking().Where("Oid == #0", oid);
//The issue with the above is when I have a ViewModel, returns something like
// = System.Data.Entity.DynamicProxies.Album_AF81C390156ACC8283ECEC668AFB22C4AD621EF70F8F64641D56852D19755BF3
//If the proper Namespace is returned, the next line works and Audit continues
var query = this.Set(entry.EntitySet.ElementType.ToString()).AsNoTracking().Where("Oid == #0", oid);
//Does a bunch of AuditLog stuff if the above issue doesn't fail
break;
}
}
}
}
catch (Exception ex)
{
throw new Exception("Log Error (" + entry.Entity.ToString() + ") - " + ex.ToString());
}
}
return base.SaveChanges();
}
}
entry.Entity.ToString() will return something like:
System.Data.Entity.DynamicProxies.Album_AF81C390156ACC8283ECEC668AFB22C4AD621EF70F8F64641D56852D19755BF3
In the AlbumView I am updating Album, and a bunch of other Objects. Not sure why it isn't returning x.Models.Music.Albums, is there a work-around, can someone explain or point me to other resources that I haven't found yet?
While it isn't the most efficient solution, it still is a solution for now.
I was able to do the following inside my db.SaveChanges() method:
//When AlbumView .BaseType was able to return x.Models.Music.Album
string strNamespace = entry.Entity.GetType().BaseType.ToString();
//Needed this if I was updating just an Object (ie: Album),
//would be nice to make something more concret
if (strNamespace == "x.Models.Core.BaseObject")
strNamespace = entry.Entity.ToString();
//Continuing code
var query = this.Set(strNamespace).AsNoTracking().Where("Oid == #0", oid);
Found the answer here from another Question that I had not found before posting this question

adding new property to asp.net identity role [duplicate]

I'm trying to use the new ASP.NET Identity in my MVC5 application, specifically I'm trying to integrate ASP.NET Identity into an existing database. I've already read the questions/answers on SO pertaining to DB First and ASP.NET Identity, and having followed all the recommendations I still can't add roles to my database, although I have no problems adding users. Here's my code:
var context = new PayrollDBEntities();
var roleManager = new RoleManager<AspNetRole>(new RoleStore<AspNetRole>(context));
bool roleExists = roleManager.RoleExists(roleDto.Name);
if (roleExists){
return false;
}
var role = new AspNetRole(roleDto.Name){
Name = roleDto.Name,
};
IdentityResult result = roleManager.Create(role);//Getting exception here
At the last line of code I get an exception of type 'System.InvalidOperationException': The entity type IdentityRole is not part of the model for the current context.
Here is my context:
public partial class PayrollDBEntities : IdentityDbContext
{
public PayrollDBEntities()
: base("name=PayrollDBEntities")
{
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
......
}
My AspNetUser and AspNetRole classes derive from IdentityUser and IdentityRole respectively, but I'm still getting that exception. Here is my database diagram:
Any help would be greatly appreciated.
You have to specify during the creation of User Store that AspNetRole is used instead of IdentityRole. You can achieve this by using the UserStore class with 6 type parameters:
new UserStore<AspNetUser, AspNetRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>(new PayrollDBEntities());
This indicates changes at User Manager creation as well. Here is a simplified example about the creation of needed instances:
public class AspNetUser : IdentityUser { /*customization*/ }
public class AspNetRole : IdentityRole { /*customization*/ }
public class PayrollDBEntities : IdentityDbContext //or : IdentityDbContext <AspNetUser, AspNetRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
}
public class Factory
{
public IdentityDbContext DbContext
{
get
{
return new PayrollDBEntities();
}
}
public UserStore<AspNetUser, AspNetRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> UserStore
{
get
{
return new UserStore<AspNetUser, AspNetRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>(DbContext);
}
}
public UserManager<AspNetUser, string> UserManager
{
get
{
return new UserManager<AspNetUser, string>(UserStore);
}
}
public RoleStore<AspNetRole> RoleStore
{
get
{
return new RoleStore<AspNetRole>(DbContext);
}
}
public RoleManager<AspNetRole> RoleManager
{
get
{
return new RoleManager<AspNetRole>(RoleStore);
}
}
}
After a few days of trying to get this to work in a clean manner, I've come to the conclusion that if you're using Database first and want to integrate ASP.NET Identity into your app, by far the easiest and cleanest solution is to create your own membership provider by overriding ASP.NET Identity. It's actually pretty easy, so far I've implemented UserStore and RoleStore to my liking. I've added columns/relations specific to my domain in my database, and whenever I create a user or a role, I take care of my database commits by adding the required relations. My UserStore implementation is quite similar to this. My RoleStore implementation is something like this:
public class ApplicationRoleStore : IRoleStore<ApplicationRoleDTO>
{
private PayrollDBEntities _context;
public ApplicationRoleStore() { }
public ApplicationRoleStore(PayrollDBEntities database)
{
_context = database;
}
public Task CreateAsync(ApplicationRoleDTO role)
{
if (role == null)
{
throw new ArgumentNullException("RoleIsRequired");
}
var roleEntity = ConvertApplicationRoleDTOToAspNetRole(role);
_context.AspNetRoles.Add(roleEntity);
return _context.SaveChangesAsync();
}
public Task DeleteAsync(ApplicationRoleDTO role)
{
var roleEntity = _context.AspNetRoles.FirstOrDefault(x => x.Id == role.Id);
if (roleEntity == null) throw new InvalidOperationException("No such role exists!");
_context.AspNetRoles.Remove(roleEntity);
return _context.SaveChangesAsync();
}
public Task<ApplicationRoleDTO> FindByIdAsync(string roleId)
{
var role = _context.AspNetRoles.FirstOrDefault(x => x.Id == roleId);
var result = role == null
? null
: ConvertAspNetRoleToApplicationRoleDTO(role);
return Task.FromResult(result);
}
public Task<ApplicationRoleDTO> FindByNameAsync(string roleName)
{
var role = _context.AspNetRoles.FirstOrDefault(x => x.Name == roleName);
var result = role == null
? null
: ConvertAspNetRoleToApplicationRoleDTO(role);
return Task.FromResult(result);
}
public Task UpdateAsync(ApplicationRoleDTO role)
{
return _context.SaveChangesAsync();
}
public void Dispose()
{
_context.Dispose();
}
private ApplicationRoleDTO ConvertAspNetRoleToApplicationRoleDTO(AspNetRole aspRole)
{
return new ApplicationRoleDTO{
Id = aspRole.Id,
EnterpriseId = aspRole.EnterpriseId,
Name = aspRole.Name
};
}
private AspNetRole ConvertApplicationRoleDTOToAspNetRole(ApplicationRoleDTO appRole)
{
return new AspNetRole{
Id = appRole.Id,
EnterpriseId = appRole.EnterpriseId,
Name = appRole.Name,
};
}
}
And my ApplicationRoleDTO:
public class ApplicationRoleDTO : IRole
{
public ApplicationRoleDTO()
{
Id = Guid.NewGuid().ToString();
}
public ApplicationRoleDTO(string roleName)
: this()
{
Name = roleName;
}
public string Id { get; set; }
public string Name { get; set; }
public Guid EnterpriseId { get; set; }
}
I also found these 2 articles pretty helpful:
Overview of Custom Storage Providers for ASP.NET Identity
Implementing a Custom MySQL ASP.NET Identity Storage Provider
I'll explain here with the code exampels :).
The trick is, they are already in the IdentityDbContext (AspNetRoles, AspNetUserClaims, AspNetUsers, ....)
In the IdentityModel you will see ApplicationUser is empty at the top. If you want to customize these users or roles, just add properties here and then update your database via the console
Example of my context
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Request> Requests { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<PriceType> PriceTypes { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Price> Prices { get; set; }
public DbSet<GuestbookPost> Posts { get; set; }
public DbSet<Count> Counts { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<InvoiceLine> InvoiceLines { get; set; }
...
}
So no application user is defined here, but I did add more properties to it, example:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string GroupName { get; set; }
public string Email { get; set; }
[StringLength(15)]
public string Phone { get; set; }
public string Remark { get; set; }
public DateTime? BirthDate { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime ValidUntil { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public virtual ICollection<Request> Requests { get; set; }
}
I know this is an old question, but just in case someone else is having a hard time adding roles/users when they modified asp identity to use numeric primary keys (int/long) instead of the default string for the Identity Roles, so if you have changed the IdentityUserRole in IdentityModels.cs to something like this:
public class Role : IdentityRole<long, UserRole>
{
public Role() { }
public Role(string name) { Name = name; }
}
You have to use the class Role instead of the default IdentityRole when constructing the RoleManager, so your code should be like this:
public static void RegisterUserRoles()
{
ApplicationDbContext context = new ApplicationDbContext();
var RoleManager = new RoleManager<Role, long>(new RoleStore(context));
if (!RoleManager.RoleExists("Administrador"))
{
var adminRole = new Role {
Name = "Administrador",
};
RoleManager.Create(adminRole);
}
}
So this should populate your database properly, I think all experienced ASP programmers already know this, but for others this could take some time to figure out.
I solved with a different way.
First I splited in two different Projects and Contexts.
My project that Handle the Identity has this context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDisposable
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
This is my ApplicationUser:
public class ApplicationUser : IdentityUser
{
//Put here the extra properties that Identity does not handle
[Required]
[MaxLength(150)]
public string Nome { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
And my ApplicationUserManager looks like this:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
//Setting validator to user name
UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
//Validation Logic and Password complexity
PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
//Lockout
UserLockoutEnabledByDefault = true;
DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
MaxFailedAccessAttemptsBeforeLockout = 5;
// Providers de Two Factor Autentication
RegisterTwoFactorProvider("Código via SMS", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Seu código de segurança é: {0}"
});
RegisterTwoFactorProvider("Código via E-mail", new EmailTokenProvider<ApplicationUser>
{
Subject = "Código de Segurança",
BodyFormat = "Seu código de segurança é: {0}"
});
//Email service
EmailService = new EmailService();
// Definindo a classe de serviço de SMS
SmsService = new SmsService();
var provider = new DpapiDataProtectionProvider("Braian");
var dataProtector = provider.Create("ASP.NET Identity");
UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtector);
}
}
I hope that this helps someone.
This solution was from this article:
Eduardo Pires - But it is in Portuguese
I fixed this issue by changing the web.config DefaultConnection connectionString property so it points to the new SQLServer database

How to automatically add a one-to-many object

I'm new at ASP.NET MVC and I'm having problems trying to insert a object 1xn automatically usining my method Create. I want to know how to insert the object RelUserHomes and I need the homeId but it'll exist only after the db.SaveChanges().
Here's a snippet:
Controller Homes
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "HomeId,Name")] Home home)
{
if (ModelState.IsValid)
{
db.Homes.Add(home);
RelUserHomes relUserHomes = new RelUserHomes();
// I did this because I need the homeId to insert this object
relUserHomes.HomeId = db.SaveChanges();
relUserHomes.Email = Session["Email"].ToString();
relUserHomes.IsAdmin = true;
db.RelUserHomes.Add(relUserHomes);
db.SaveChanges();
}
return View(home);
}
Model RelUserHomes
public class RelUserHomes
{
[Key]
public int RelUserHomesId { get; set; }
public bool IsAdmin { get; set; }
[Required]
public string Email { get; set; }
[Required]
public int HomeId { get; set; }
[Key]
public Home Homes { get; set; }
}
Application DBContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("PrimoEntities")
{
}
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
var fullErrorMessage = string.Join("; ", errorMessages);
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
}
}
public DbSet<Home> Homes { get; set; }
public DbSet<RelUserHomes> RelUserHomes { get; set; }
}
If a RelUserHomes entity has a foreign key relationship pointing to a Home entity, the Home object must be added to the database first, so that it is assigned a HomeId value. You can simply add a db.SaveChanges() a little earlier in the code, and then assign relUserHomes the HomeId property of Home.
Try changing your code to the following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "HomeId,Name")] Home home)
{
if (ModelState.IsValid)
{
db.Homes.Add(home);
db.SaveChanges();
RelUserHomes relUserHomes = new RelUserHomes();
relUserHomes.HomeId = home.HomeId;
db.RelUserHomes.Add(relUserHomes);
db.SaveChanges();
}
return View(home);
}

Categories