0

I am trying to insert data in my DB for registration and below is what I have inside my AccountController

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(PersonLogin rd)
    {
        EBYSContext db = new EBYSContext();
        if (ModelState.IsValid)
        {
            var kisi = new Person();
            kisi.Adi = "Abdullah";
            kisi.TcKimlikNo = "99565282133";
            kisi.Soyadi = "Taher";
            db.Persons.Add(kisi);
            db.SaveChanges();


            WebSecurity.CreateUserAndAccount(rd.Username, rd.Password, new
            {

                //KisiID = new PersonLogin(),  
                Username = rd.Username,
                Password = rd.Password,
                Email = rd.Email,
                YanlisGirisSayisi = 1,
                SifreDegistirilsin = false,
                SonKullanimTarihi = new DateTime(2014, 1, 18),
                SonGirisTarihi = new DateTime(2014, 1, 18),
                SonSifreDegisimTarihi = new DateTime(2014, 1, 18),

            });

            return RedirectToAction("Login","Account");
        }

        ModelState.AddModelError("","Please check your credentials...");
        return View(rd);
    }

I have two two tables Person and PersonLogin. The later contain login information like NumberOfLoginAttempts(YanlisGirisSayisi ) etc. The two table has 1-1 relationship.

When I post the form I am able to insert the data in Person's table but later I get Cannot convert type 'System.Guid' to 'int', the error being pointing to

WebSecurity.CreateUserAndAccount(rd.Username, rd.Password, new
{

After a long research I came to realize that data for userIdColumn in WebSecurity.InitializeDatabaseConnection MUST be integer. I have declared my userId (kisiID) column as uniqueIdentifierin database. As far as I know it is impossible to convert System.Guid to int My question is how can I go about inserting a new row in PersonLogin table using the same ID created in Person table.

I know the question is little bit confusing but I hope I'll get something useful. By the way am using Entity framework Database First Approach.

Additonal Links: InitializeDatabaseConnection Method

Mussa Moses
  • 101
  • 9
  • Guid cannot be represented as int – Ehsan Sajjad Apr 09 '15 at 13:55
  • So what should I do , I do not need to change my UserId column from uniqueIdentifier to int – Mussa Moses Apr 09 '15 at 13:57
  • On c# side you can hold Guid in string – Ehsan Sajjad Apr 09 '15 at 13:59
  • 1
    @EhsanSajjad Why not keep a Guid as a Guid in C#? – DavidG Apr 09 '15 at 14:03
  • I might be a little slow on picking this up, but you are rolling your own membership tables (`Person` and `PersonLogin`) but then you are still trying to use the internal membership system as well? And you want the `Guid` from the `Person` table to also be the ID in the internal membership tables? But the internal table uses `int` instead of `Guid`. Is that right? – jwatts1980 Apr 09 '15 at 14:16
  • Absolutely @jwatts1980 – Mussa Moses Apr 09 '15 at 14:20
  • Can you not use the user name as the link between the two? I have done it this way in the past in an ASP.NET WebForms website. The internal membership system has several "get by username" methods. You could modify the login action to first authenticate against your table, then if successful, use the username to set the user as the currently logged in user. – jwatts1980 Apr 09 '15 at 14:24
  • In my latest MVC project, I gutted the built-in membership system and rolled my own. It was a large project and I needed very granular control over the users, roles, and permissions. This SO answer provided some great links to get me started: http://stackoverflow.com/a/15083137/579148 Though it might be overkill for your purposes. – jwatts1980 Apr 09 '15 at 14:32
  • I am using userID or kisiID (as I have declared) not username to link two tables. Sorry for bad naming convention "PersonLogin" hold all data for registering the user (in the case above). Am trying to take screenshot of all code. i know it is a bit confusing for now. – Mussa Moses Apr 09 '15 at 14:32
  • Thanks for the links @jwatts1980 ...I found one which resembles what am looking for.. – Mussa Moses Apr 09 '15 at 14:35

0 Answers0