Skip to content

Instantly share code, notes, and snippets.

@abdulmajedsulim
Last active May 2, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulmajedsulim/04afdd783c2d0f999d670876521bdeba to your computer and use it in GitHub Desktop.
Save abdulmajedsulim/04afdd783c2d0f999d670876521bdeba to your computer and use it in GitHub Desktop.
Issue in OrmLite
using ServiceStack.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StoreSystem.Core
{
public class BaseEntity
{
[PrimaryKey]
[AutoIncrement]
public int Id { get; set; }
[CustomField("nvarchar(4000)")]
public string Name { get; set; }
[CustomField("nvarchar(4000)")]
public string Description { get; set; }
public bool IsDeleted { get; set; }
public byte[] Version { get; set; }
public int? CreatedByID { get; set; }
public DateTime? CreatedAt { get; set; }
public int? UpdatedByID { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class Store : BaseEntity
{
[CustomField("NVARCHAR(4000)")]
public string StoreNumber { get; set; }
[Reference]
public List<Authorizers> Authorizers { get; set; }
[CustomField("NVARCHAR(4000)")]
public string ClientName { get; set; }
[CustomField("NVARCHAR(20)")]
public string ClientMobile { get; set; }
[CustomField("NVARCHAR(4000)")]
public string ClientEmail { get; set; }
public void Update()
{
UnitOfWork.Obj.DB.Save(this, true);
}
}
public class Authorizers : BaseEntity
{
[ForeignKey(typeof(Store),OnDelete = "CASCADE")]
public int StoreID { get; set; }
public int CaseAuthorizerId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace Store.WebApp.Controllers
{
[Authorize()]
public class StoreController : Controller
{
public ActionResult SaveStore(StoreModel model)
{
var authorizersList = new List<Authorizers>();
foreach (var item in model.Authorizers)
{
authorizersList.Add(new Authorizers() { CaseAuthorizerId = item });
}
Store mystore = new Stroe();
mystore.Authorizers=authorizersList;
myStore.Update();
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.OrmLite;
namespace Store.Services
{
public class UnitOfWork
{
public IDbConnection DB { get; set; }
private OrmLiteConnectionFactory DbFactory;
public static UnitOfWork Obj { get; private set; }
private static string CnnStr = System.Configuration.ConfigurationManager.ConnectionStrings["CnnStr"].ConnectionString;
static UnitOfWork()
{
Obj = new UnitOfWork(CnnStr);
}
public UnitOfWork(string connectionString)
{
if(DbFactory == null)
{
DbFactory = new OrmLiteConnectionFactory(CnnStr, SqlServer2016Dialect.Provider);
OrmLiteConfig.DialectProvider.GetStringConverter().UseUnicode = true;
}
DB = DbFactory.OpenDbConnection();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment