11 Ocak 2015 Pazar

How To Use Fluent Nhibernate and Mysql and C# Together. Fluent Nhibernate Configuration in Visual Studio C# and Mysql Example.

Hello my friends , i will show you how to use Fluent Nhibernate with Mysql and C# together as an Orm Tool in that post.First Create your C# project and add 2 folders Map and Model Folders to your project.Then either Add Fluent Nhibernate binaries manually or just open Nuget Packet Manager Console and Run " Install-Package FluentNHibernate " command. Then Add Classes to Model Folder as your Models representing your Tables in your DB. Such As ; Say that you have User Table with Primary Key UserID and varchar UserName and varchar Password Fields. Only important Point is that you must use virtual keyword in your properties othervise you will have errors. lets see User Table 's Model Class;

 public class UserModel
    {

        public virtual int UserID { get; set; }

        public virtual string  UserName { get; set; }

        public virtual string Password { get; set; }
    }

then in your Map Folders Create a Class as UserMap. Here we have some points to be careful . First You Will Extend ClassMap and Make Your Mappings in Code Side at the Constructor.Just Like as Follows;

public class UserMap:ClassMap< UserModel >
    {
        
        public UserMap() 
        {
            Table("User");
            Id(x => x.UserID);
            Map(x => x.UserName, "UserName/*Real Db Column  Name*/");
            Map(x => x.Password, "Password/*Real Db Column  Name*/");
        }
    }

Then we completed all mapping and model works.Lets Create Configuration and Make Some Insert Update Operations and Create the DbHelper Class. Create DbHelper Class in your project.Like;

 public class DbHelper
    {
        private static ISessionFactory sessionFactory;
       

        public static ISessionFactory getSessionFactory()
        {
            if (sessionFactory == null)
            {

                sessionFactory = Fluently
                                .Configure()
                                .Database(
                                        MySQLConfiguration
                                        .Standard
                                        .ConnectionString(ConfigurationManager.ConnectionStrings["MyDbName"].ConnectionString
                                        )).Mappings(x => x.FluentMappings.AddFromAssemblyOf< UserModel >()).BuildSessionFactory();
            /* 
             Here in AddFromAssemblyOf() point , we just point the folder where our models exist , do not hesitate when choosing a model  , just pick a model it will identify the place only to collect models.I               
             mean you dont have to add all model names here only pickking one is enough.
            */

            }
            return sessionFactory;
        }


      public static bool CreateUser(UserModel newUser)
        {

            using (var session = getSessionFactory().OpenSession())
            {
                try
                {
                    session.Save(newUser);
                    session.Flush();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

        }

     public static bool UpdateUser(int userID,string newUserName)
        {

            using (var session = getSessionFactory().OpenSession())
            {
                try
                {
                    UserModel userToUpdate = (UserModel)session.Get("UserModel", userID);
                    userToUpdate.UserName=newUserName;
                    session.Update(userToUpdate);
                    session.Flush();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

        }


  }

Now It is Done Successfully , lets Create an instance of DbHelper and make an Insert Operation .

Hiç yorum yok:

Yorum Gönder