// GET: Home public ActionResult Index() { _db = new CodeFirstModel(); //_db.Entry(User).State = System.Data.Entity.EntityState.Modified; _db.SaveChanges(); return(View()); }
static void Main(string[] args) { //THIS IS FOR DATABASEFIRST //EFDatabaseFirstEntities context = new EFDatabaseFirstEntities(); //foreach (Broker broker in context.Brokers) //{ // Console.WriteLine(broker.name); //} //Console.ReadLine(); //THIS IS FOR CODEFIRST CodeFirstModel context = new CodeFirstModel(); //Add //Broker newBroker = new Broker() { name = "Bishan", companyId = 1 }; //context.brokers.Add(newBroker); //Update //Broker brokerToUpdate = context.brokers.Find(1); //brokerToUpdate.name = "Ben"; //Remove //foreach (Broker broker in context.brokers) //{ // if (broker.id == 3) // { // context.brokers.Remove(broker); // } //} //context.SaveChanges(); //foreach (Broker broker in context.brokers) //{ // Console.WriteLine(broker.name, broker.companyId); //} //LINQ SQL var query = from b in context.brokers where b.companyId == 1 select b; foreach (var broker in query) { Console.WriteLine(broker.name + " " + broker.companyId); } Console.ReadLine(); }
static void Main(string[] args) { //THIS IS FOR DATABASE FIRST //EFDemoDatabaseFirstEntities context = new EFDemoDatabaseFirstEntities(); //foreach (broker broker in context.brokers) //{ // Console.WriteLine(broker.name); //} //THIS IS FOR CODE FIRST CodeFirstModel context = new CodeFirstModel(); //Adding something to the database //Broker newBroker = new Broker() { name = "Bishan", companyId = 1 }; //context.brokers.Add(newBroker); //context.SaveChanges(); //IN CODE HAVE ONLY ONE SAVE CHANGES SO ONLY GOING THROUGH DATABASE ONCE //Finished code to add broker //Updating Broker //Broker brokerToUpdate = context.brokers.Find(2); //brokerToUpdate.name = "Luffy"; //context.SaveChanges(); //Finishing code to update broker //Removing Broker //foreach (Broker broker in context.brokers) //{ // if (broker.id == 3) // { // context.brokers.Remove(broker); // } //} //context.SaveChanges(); //need to find a better way, this one is hardcoded. //Finishing code to remove broker //foreach (Broker broker in context.brokers) //{ // Console.WriteLine(broker.name); //} //Using Linq statement var query = from b in context.brokers where b.companyId == 1 select b; foreach (var broker in query) { Console.WriteLine(broker.name); } //Finishing using Linq. Returns broker names for whom the company id is 1. Console.ReadLine(); }
/// <summary> /// 获取模型信息方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static CodeFirstModel GetModelData<T>() where T : class { CodeFirstModel codeFirstModel = new CodeFirstModel(); codeFirstModel.ClassName = typeof(T).Name; var attr = CodeFirstAttributeHelper.GetCustomAttributeForClass<T, ModelAttributes.ItemDisplayNameAttribute>(); if (attr != null) { codeFirstModel.Title = attr.DisplayName; } codeFirstModel.CodeFirstItemList = CodeFirstAttributeHelper.GetModelAttributeInfo<T>(); return codeFirstModel; }
/// <summary> /// 获取模型信息方法 /// </summary> /// <param name="modelType"></param> /// <returns></returns> public static CodeFirstModel GetModelData(Type modelType) { CodeFirstModel codeFirstModel = new CodeFirstModel(); codeFirstModel.ClassName = modelType.Name; var attr = GetModelAttribute< ModelAttributes.ItemDisplayNameAttribute>(modelType); if (attr != null) { codeFirstModel.Title = attr.DisplayName; } codeFirstModel.CodeFirstItemList = CodeFirstAttributeHelper.GetModelAttributeInfo(modelType); return codeFirstModel; }
/// <summary> /// Creates a new repository connection to a database /// </summary> /// <returns></returns> public IRepository Create() { // Load connection string var config = ConfigurationManager.ConnectionStrings["Data.Properties.Settings.BlackBallArchitectureConnectionString"]; if (config == null) { throw new Exception("Data.Properties.Settings.BlackBallArchitectureConnectionString has not been configured"); } this.ConnectionString = config.ConnectionString; if (string.IsNullOrWhiteSpace(this.ConnectionString)) { throw new Exception("Data.Properties.Settings.BlackBallArchitectureConnectionString has not been configured"); } // Create and return connection Database.SetInitializer <CodeFirstModel>(null); var result = new CodeFirstModel(this.ConnectionString); result.ApplicationName = this.ApplicationName; result.CommandTimeout = DefaultCommandTimeout; return(result); }