Save user profile information _userManager.UpdateAsync(user); - asp.net

I'm trying to get Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6 to write some information to the current ApplicationUser "profile" but the value is null and I don't get any exceptions running the following code.
public class TestController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
public TestController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
// GET: /<controller>/
public async Task<ActionResult> Index()
{
var userObject = await _userManager.FindByIdAsync(Context.User.GetUserId());
//This value is null even the second time I hit this breakpoint.
var shouldHaveAValue = userObject.MyStringList;
userObject.MyStringList = new List<string>();
userObject.MyStringList.Add("testId");
await _userManager.UpdateAsync(userObject);
return View();
}
}
I might be completely wrong here but this is the way I figured it should be done when I saw
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public virtual List<string> MyStringList { get; set; }
}
The example is implemented in the ASP.NET5 web template.
//Edit:
Could it be the way I added the column? I did this since I get errors running the migrations commands which is stated not to be ready on the ef documentation page.( dnx . ef apply )
migration.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column(type: "nvarchar(450)", nullable: false),
AccessFailedCount = table.Column(type: "int", nullable: false),
ConcurrencyStamp = table.Column(type: "nvarchar(max)", nullable: true),
Email = table.Column(type: "nvarchar(max)", nullable: true),
EmailConfirmed = table.Column(type: "bit", nullable: false),
LockoutEnabled = table.Column(type: "bit", nullable: false),
LockoutEnd = table.Column(type: "datetimeoffset", nullable: true),
NormalizedEmail = table.Column(type: "nvarchar(max)", nullable: true),
NormalizedUserName = table.Column(type: "nvarchar(max)", nullable: true),
PasswordHash = table.Column(type: "nvarchar(max)", nullable: true),
PhoneNumber = table.Column(type: "nvarchar(max)", nullable: true),
PhoneNumberConfirmed = table.Column(type: "bit", nullable: false),
SecurityStamp = table.Column(type: "nvarchar(max)", nullable: true),
TwoFactorEnabled = table.Column(type: "bit", nullable: false),
UserName = table.Column(type: "nvarchar(max)", nullable: true),
MyStringList = table.Column(type: "nvarchar(max)", nullable: true)
},

Related

oneToMany JPA spring

I am trying to link two errors, but I am falling out
Here's a error:
mappedBy reference an unknown target entity property: com.proxy.ProxyBase.proxy_server in com.accounts.AllAccountsBase.proxies
First model
#Data
#Entity
#Table(name = "all_accounts")
public class AllAccountsBase {
#Id
#Column(name = "login", nullable = false, unique = true)
private String login;
#Column(name = "password")
private String password;
#Column(name = "old_password")
private String oldPassword;
#Column(name = "phone")
private String phone;
#Column(name = "recovery_email")
private String recoveryEmail;
#Column(name = "recovery_pass")
private String recoveryPass;
#Column(name = "virt_machine")
private String machineName;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "personal_data_id")
private PersonalData personalData;
#Column(name = "account_status")
private Integer accountStatus;
#Column(name = "time_update")
private Timestamp timeUpdate;
#Column(name = "server_group")
private String serverGroup;
#Column(name = "server", nullable = false, unique = true)
private String server;
#OneToMany(
mappedBy = "proxy",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
private Collection<ProxyBase> proxies = new ArrayList<>();
}
In the model, I added a relationship one to many, and vice versa in the other
Second model
#Data
#Entity
#Table(name = "proxy")
public class ProxyBase implements Serializable {
#Id
#Column(name = "server", nullable = false, unique = true)
private String server;
#Column(name = "proxy_data")
private String proxyData;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "all_accounts_server")
private AllAccountsBase allAccountsBase;
}
Help please
I did it differently:
#Data
#Entity
#Table(name = "proxy")
public class ProxyBase implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Column(name = "server", nullable = false)
private String server;
#Column(name = "proxy_data")
private String proxyData;
}
and
#Data
#Entity
#Table(name = "all_accounts")
public class AllAccountsBase implements Serializable {
#Id
#Column(name = "login", nullable = false, unique = true)
private String login;
#Column(name = "password")
private String password;
#Column(name = "old_password")
private String oldPassword;
#Column(name = "phone")
private String phone;
#Column(name = "recovery_email")
private String recoveryEmail;
#Column(name = "recovery_pass")
private String recoveryPass;
#Column(name = "virt_machine")
private String machineName;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "personal_data_id")
private PersonalData personalData;
#Column(name = "account_status")
private Integer accountStatus;
#Column(name = "time_update")
private Timestamp timeUpdate;
#Column(name = "server_group", nullable = false)
private String serverGroup;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(
name = "proxy_key",
joinColumns = #JoinColumn(name = "server_group", referencedColumnName = "server_group"),
inverseJoinColumns = #JoinColumn(name = "proxy_server_group", referencedColumnName = "server"))
private Collection<ProxyBase> proxies = new ArrayList<>();
}

Invalid column name in Entity Framework

I have an ASP.MET application. I performed the tutorial on code first migrations. When I executed the commands, the following code was generated for the user table:
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
ApplicationId = c.Guid(nullable: false),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: true),
UserName = c.String(nullable: false, maxLength: 256),
Discriminator = c.String(),
LegacyPasswordHash = c.String(),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
My problem is that I don't need some columns. So, I removed the code from those columns and updated my DB. But, when I try to authenticate a user, I get this error:
Invalid column name 'EmailConfirmed'.
Invalid column name 'PhoneNumberConfirmed'.
Invalid column name 'TwoFactorEnabled'.
Invalid column name 'LockoutEndDateUtc'.
Invalid column name 'LockoutEnabled'.
Invalid column name 'AccessFailedCount'.
But these columns no longer exist because I deleted them.
Or, is it possible to put it back and assign them default values ​​for example:
EmailConfirmed = false
PhoneNumberConfirmed = false
to avoid making an insertion when creating a user. I use EF 6.2.0
Need help please.

Migration Failed ASP.Net API

I have problem when I'm trying to update the database.
I'm making web-api with asp.net.
I just added new migration and works perfectly. Then updated my database with update-database command. Then I change some codes. When I add new migration it said:
Unable to generate an explicit migration because the following explicit migrations are pending:
[201705310248355_changeProduct, 201706191007322_ModifyModel, 201707170631209_changeProductModel, 201707170652556_EditProductModelLagi, 201707180312064_AddStorageIdOnInventory, 201707190243206_ChangeRepository, 201708091124374_EditInventory, 201803130702299_testBoom].
Apply the pending explicit migrations before attempting to generate a new explicit migration.
but last time I updated the database it works perfectly
PM> update-database -target testboom -startupprojectname ShopDiaryProject.EF -force
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is already at version 201803130702299_testBoom.
Soo, I tried to add-migration with -force parameter.
It works!
BUT,
I update the database again, the migration content likes start from beginning and can't add tables.
Here are my migration:
public override void Up()
{
CreateTable(
"dbo.Categories",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(maxLength: 200),
Description = c.String(maxLength: 200),
UserId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
User_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.User_Id);
CreateTable(
"dbo.Products",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(maxLength: 250),
BarcodeId = c.String(maxLength: 250),
CategoryId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Categories", t => t.CategoryId, cascadeDelete: true)
.Index(t => t.CategoryId);
CreateTable(
"dbo.Shopitems",
c => new
{
Id = c.Guid(nullable: false),
Quantity = c.Int(nullable: false),
price = c.Decimal(nullable: false, precision: 18, scale: 2),
ProductId = c.Guid(nullable: false),
ShoplistId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
})
and it said There is already an object named 'Categories' in the database.
Any ideas?

How to prevent duplicate validation while updating the value without any changes?

#RequestMapping(value = "/updateYearMaster", method = RequestMethod.POST)
public String updateYearmaster(#RequestParam(value = "id", required = false) Long id,
#RequestParam(value = "fromyear", required = false) Date fromyear,
#RequestParam(value = "toyear", required = false) Date toyear,
#RequestParam(value = "status", required = false) String status,
#RequestParam(value = "yeardescription", required = false) String yeardescription, Model model) {
Yearmaster yearmaster = new Yearmaster(fromyear, toyear, status, yeardescription);
yearmaster.setId(id);
List val = yearmasterService.duplicateEditYear(fromyear, toyear, id);
if (!val.isEmpty()) {
model.addAttribute("yearmaster", yearmaster);
errorMessage = "fromyear and toyear combination is already exist";
model.addAttribute("errorMessage", errorMessage);
return "edit-year-master";
} else {
yearmasterService.save(yearmaster);
return "redirect:/yearmaster";
}
}

Foreign key between tbls

i create Table with Code First and create table with fluentapi .
how can i create Foreign key between them ?
public partial class Tbl_Gender
{
[Key]
public int GendID { get; set; }
[Required]
[StringLength(150)]
public GenType GendType { get; set; }
}
public enum GenType
{
Male = 1,
Fmale = 2
}
}
.
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(),
Family = c.String(),
UserPhoto = c.String(),
RoleID = c.String(),
Gender = c.String(),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
/*******************************************************************************/

Resources