• UserProfile
  • Dasturiy injiniring (EntityFramework 6)




    Download 1,39 Mb.
    bet20/53
    Sana30.11.2023
    Hajmi1,39 Mb.
    #108476
    1   ...   16   17   18   19   20   21   22   23   ...   53
    Bog'liq
    Dasturiy injiniring

    Yangi yozuv qo’shish va o’qib olish:
    static void Main(string[] args)
    {
    using (UserContext db = new UserContext())
    {
    User user1 = new User { Login = "login1", Password = "pass1234" };
    User user2 = new User { Login = "login2", Password = "5678word2" };
    db.Users.AddRange(new List { user1, user2 });
    db.SaveChanges();
    UserProfile profile1 = new UserProfile { Id = user1.Id, Age = 22, Name = "Ahmad" };
    UserProfile profile2 = new UserProfile { Id = user2.Id, Age = 27, Name = "Salim" };
    db.UserProfiles.AddRange(new List { profile1, profile2 });
    db.SaveChanges();

    foreach (User user in db.Users.Include("Profile").ToList())


    Console.WriteLine("Name: {0} Age: {1} Login: {2} Password: {3}",
    user.Profile.Name, user.Profile.Age, user.Login, user.Password);
    }
    Console.ReadLine();
    }
    Tahrirlash
    static void Main(string[] args)
    {
    using (UserContext db = new UserContext())
    {
    User user1 = db.Users.FirstOrDefault();
    if (user1 != null)
    {
    user1.Password = "dsfvbggg";
    db.Entry(user1).State = EntityState.Modified;
    db.SaveChanges();
    }

    UserProfile profile2 = db.UserProfiles.FirstOrDefault(p => p.User.Login == "login2");


    if (profile2 != null)
    {
    profile2.Name = "Alice II";
    db.Entry(profile2).State = EntityState.Modified;
    db.SaveChanges();
    }
    }
    Console.ReadLine();
    }

    Modelda o’chirish amalini bajarishda UserProfile ob’ekti User ob’ektining mavjud bo’lishini talab qiladi. Shu sababli, User ob’ekti o’chirilishi natijasida UserProfile ob’ekti ham o’chiriladi. UserProfile ob’ekti o’chirilishi natijasida User ob’ekti o’chirilmaydi.


    static void Main(string[] args)


    {
    using (UserContext db = new UserContext())
    {
    User user1 = db.Users.Include("Profile").FirstOrDefault();
    if (user1 != null)
    {
    db.UserProfiles.Remove(user1.Profile);
    db.Users.Remove(user1);
    db.SaveChanges();
    }

    UserProfile profile2 = db.UserProfiles.FirstOrDefault(p => p.User.Login == "login2");


    if (profile2 != null)
    {
    db.UserProfiles.Remove(profile2);
    db.SaveChanges();
    }
    }
    Console.ReadLine();
    }



    Download 1,39 Mb.
    1   ...   16   17   18   19   20   21   22   23   ...   53




    Download 1,39 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Dasturiy injiniring (EntityFramework 6)

    Download 1,39 Mb.