本文整理汇总了C#中DBContext.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# DBContext.SaveChanges方法的具体用法?C# DBContext.SaveChanges怎么用?C# DBContext.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBContext
的用法示例。
在下文中一共展示了DBContext.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
public HttpResponseMessage Add(List<Gallery> items)
{
try
{
foreach (var item in items)
{
List<string> content = item.Content.Split(',').ToList();
var bytes = Convert.FromBase64String(content[1]);
string mappath = HttpContext.Current.Server.MapPath("~/Assets/img/gallery/");
var filepath = Path.Combine(mappath, item.Name);
//string filepath = String.Format(mappath + "{0}", item.Name);
using (var imageFile = new FileStream(filepath, FileMode.Create))
{
imageFile.Write(bytes, 0, bytes.Length);
imageFile.Flush();
}
item.Content = String.Format("Assets/img/gallery/{0}", item.Name);
}
using (var db = new DBContext())
{
db.Galleries.AddRange(items);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Accepted);
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest,ex.Message);
}
}
示例2: AddLancamento
public HttpResponseMessage AddLancamento([FromBody]JObject model)
{
try
{
dynamic json = model["Lancamento"];
DBContext db = new DBContext();
int idEmpresa = 0; idEmpresa = json.idEmpresa;
var dataVenda = DateTime.Now;
double valorVenda = json.valorVenda;
var cnh = "";
cnh = json.cnh;
BomoCliente cliente = new BomoCliente();
var query = from cli in db.BomoCliente
where cli.cnh == cnh
select cli;
foreach(BomoCliente bc in query)
{
cliente = bc;
}
if (cliente.idBomoCliente < 1)
{
cliente = new BomoCliente();
cliente.cnh = cnh;
db.BomoCliente.Add(cliente);
db.SaveChanges();
}
BomoLancamentos lanc = new BomoLancamentos();
lanc.BomoCliente = db.BomoCliente.Single(x => x.idBomoCliente == cliente.idBomoCliente);
lanc.BomoEmpresa = db.BomoEmpresa.Single(x => x.idBomoEmpresa == idEmpresa);
lanc.dataVenda = dataVenda;
lanc.valorVenda = valorVenda;
db.BomoLancamentos.Add(lanc);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, new { });
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例3: GetGallery
public HttpResponseMessage GetGallery()
{
try
{
using (var db = new DBContext())
{
var result = db.Galleries.ToList();
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Accepted, result);
}
}
catch (Exception)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
示例4: Delete
public HttpResponseMessage Delete(int ID)
{
try
{
using (var db = new DBContext())
{
db.Galleries.Remove(db.Galleries.First(g => g.ID == ID));
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Accepted);
}
}
catch (Exception)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
示例5: CadastroCategoria
public HttpResponseMessage CadastroCategoria(Categoria c)
{
try
{
using (DBContext db = new DBContext())
{
db.Database.CreateIfNotExists();
db.Categoria.Add(c);
db.SaveChanges();
}
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro da categoria " + c.nomeCategoria + " realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例6: AddEmpregado
public HttpResponseMessage AddEmpregado(EmpresaCliente c)
{
try
{
using (DBContext db = new DBContext())
{
db.Database.CreateIfNotExists();
db.EmpresaCliente.Add(c);
db.SaveChanges();
}
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro do servico " + c.cliente.nomeCliente + " na empresa " + c.empresa.nomeEmpresa + " realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例7: ImportCountriesAndNamesIntoDB
public void ImportCountriesAndNamesIntoDB()
{
var countryService = new CountryService();
var nameService = new NameService();
Trace.WriteLine("Create DB if not exists");
System.Data.Entity.Database.SetInitializer(new DBInitializer());
Trace.WriteLine("Start DB sync");
List<Country> countries = countryService.GetCountriesAsync().Result;
using (var dbContext = new DBContext())
{
dbContext.BulkInsert(countries);
dbContext.SaveChanges();
int totalCountries = countries.Count();
int countriesCount = 0;
Trace.WriteLine($"DB sync: {totalCountries} countries downloaded");
foreach (Country country in dbContext.Countries)
{
Trace.WriteLine($"DB sync: start country {country.Name} ({countriesCount++} of {totalCountries})");
BabyName[] names = nameService.GetNamesAsync(country.HRef).Result.ToArray();
foreach (BabyName name in names)
{
name.CountryId = country.Id;
}
using (var nameContext = new DBContext())
{
nameContext.BulkInsert(names);
nameContext.SaveChanges();
}
Trace.WriteLine($"DB sync: end country {country.Name}: {names.Count()} names downloaded");
}
}
Trace.WriteLine("End DB sync");
}
示例8: ImportCountryCodesIntoDB
public void ImportCountryCodesIntoDB()
{
var countryCodeService = new CountryCodeService();
IDictionary<string, string> countryCodes = countryCodeService.GetCountryCodesAsync().Result;
using (var db = new DBContext())
{
foreach (Country country in db.Countries)
{
string countryCode = countryCodes.SingleOrDefault(x => x.Key == country.Name).Value;
if (countryCode != null)
{
country.CountryCode = countryCode;
}
}
db.SaveChanges();
}
}
示例9: CadastroCliente
public HttpResponseMessage CadastroCliente([FromBody]JObject model)
{
try
{
//System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
//var data = js.Deserialize<Cliente>(dataCliente);
var cliente = new BomoCliente();
DBContext db = new DBContext();
dynamic json = model;
cliente = json.BomoCliente.ToObject<BomoCliente>();
if (cliente.cnh == null) cliente.cnh = "";
db.BomoCliente.Add(cliente);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, new { cliente.idBomoCliente });
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例10: CadastroServicoCliente
public HttpResponseMessage CadastroServicoCliente([FromBody]JObject model)
{
try
{
dynamic json = model["Horario"];
int idUsuario = 0;
Cliente cliente = new Cliente() ;
string dataServico = json.dataServico;
idUsuario = json.idUsuario;
DBContext db = new DBContext();
var query = from user in db.Usuario
where user.idUsuario == idUsuario
select user;
foreach(Usuario x in query)
{
cliente = x.cliente;
}
ClienteServico cs = new ClienteServico()
{
dataServico = Convert.ToDateTime(json.dataServico),
empresaCliServ = json.empresaServico,
valorServico = json.valorServico,
cliente = cliente
};
db.Database.CreateIfNotExists();
db.ClienteServico.Add(cs);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例11: ListarEmpresas
public HttpResponseMessage ListarEmpresas([FromBody]JObject model)
{
try
{
List<Empresa> empresas = new List<Empresa>();
dynamic json = model;
DBContext db = new DBContext();
string cidadeCliente = "";
string estadoCliente = "";
int idCategoria = 0;
cidadeCliente = json.cidadeCliente;
estadoCliente = json.estadoCliente;
idCategoria = json.idCategoria;
var servs = from es in db.EmpresaServico where es.servico.categoria.idCategoria == idCategoria
select es.empresa.idEmpresa;
var query = from e in db.Empresa
where e.cidadeEmpresa == cidadeCliente
&& e.estadoEmpresa == estadoCliente
&& servs.Contains(e.idEmpresa)
select e;
//db.Empresa.SqlQuery("select e.* from Empresas e " +
//" where e.idEmpresa in ( " +
//" select es.idEmpresa from EmpresaServico es where " +
//" es.idServico in (select s.idServico from Servicos s where idCategoria = " + json.idCategoria + ")) " +
//" and e.cidadeEmpresa = (Select cli.cidadeCliente from Clientes cli " +
//" where cli.idCliente = (select u.idCliente from Usuarios u where idUsuario = " + json.idUsuario + ")) " +
//" and e.estadoEmpresa = (Select cli.estadoCliente from Clientes cli " +
//" where cli.idCliente = (select u.idCliente from Usuarios u where idUsuario = " + json.idUsuario + ")) " +
//" order by e.bairroEmpresa ");
foreach (Empresa e in query)
{
empresas.Add(e);
}
foreach (Empresa e in empresas)
{
db.SaveChanges();
var query2 = (from s in db.Servico
join es in db.EmpresaServico on e.idEmpresa equals es.empresa.idEmpresa
where s.idServico == es.servico.idServico
select s.tipoServico).Distinct();
string tipoServico = "";
foreach (string tipo in query2)
{
tipoServico += tipo + " ";
}
e.tipoServico = tipoServico;
}
return Request.CreateResponse(HttpStatusCode.OK, empresas);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例12: CadastroEmpresa
public HttpResponseMessage CadastroEmpresa([FromBody]JObject model)
{
try
{
var empresa = new Empresa();
DBContext db = new DBContext();
dynamic json = model["Empresa"];
empresa = json.ToObject<Empresa>();
db.Empresa.Add(empresa);
json = model["Cliente"];
int idCliente = json;
var user = (from u in db.Usuario
where u.cliente.idCliente == idCliente
select u).First();
user.roleUsuario = "manager";
EmpresaCliente ec = new EmpresaCliente()
{
cliente = (from c in db.Cliente
where c.idCliente == idCliente
select c).First(),
tipoCliente = 2,
empresa = empresa,
especializacaoCliente = ""
};
db.EmpresaCliente.Add(ec);
db.SaveChanges();
// idEmpresaCliente idEmpresa idCliente tipoCliente especializacaoCliente especializacao2Cliente especializacao3Cliente
//22 1 78 3 Massagista de Mulheres NULL NULL
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro da empresa " + empresa.nomeEmpresa + " realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例13: ImportStatesIntoDB
//.........这里部分代码省略.........
Points = "470,162,489,147",
});
states.Add(new State
{
CountryCode = "AS",
Pin = "13,145",
Offset = -11,
Points = "7,150,20,135",
});
states.Add(new State
{
CountryCode = "PW",
Pin = "437,115",
Offset = 9,
Points = "427,126,442,109",
});
states.Add(new State
{
CountryCode = "PN",
Pin = "69,160",
Offset = -8,
Points = "63,165,82,153",
});
states.Add(new State
{
CountryCode = "FM",
Pin = "470,115",
Offset = 11,
Points = "463,122,478,110",
});
states.Add(new State
{
CountryCode = "PG",
Pin = "454,138",
Offset = 10,
Points = "447,141,471,126",
});
states.Add(new State
{
CountryCode = "CK",
Pin = "28,154",
Offset = -10,
Points = "20,155,32,137",
});
states.Add(new State
{
CountryCode = "MP",
Pin = "452,104",
Offset = 10,
Points = "446,110,458,91",
});
states.Add(new State
{
CountryCode = "PF",
Pin = "42,149",
Offset = -10,
Points = "35,163,61,145",
});
states.Add(new State
{
CountryCode = "KI",
Pin = "490,123",
Offset = 12,
Points = "485,134,496,115",
});
states.Add(new State
{
CountryCode = "TO",
Pin = "7,154",
Offset = 13,
Points = "0,161,14,142",
});
states.Add(new State
{
CountryCode = "UM",
Pin = "481,98",
Offset = 12,
Points = "476,103,486,93",
});
states.Add(new State
{
CountryCode = "WF",
Pin = "5,143",
Offset = 12,
Points = "-3,150,10,138",
});
Trace.WriteLine($"DB sync: start states");
Trace.WriteLine("Create DB if not exists");
System.Data.Entity.Database.SetInitializer(new DBInitializer());
using (var nameContext = new DBContext())
{
nameContext.BulkInsert(states);
nameContext.SaveChanges();
}
Trace.WriteLine($"DB sync: end states");
}
示例14: SendContato
public HttpResponseMessage SendContato([FromBody]JObject modelContato)
{
try
{
//System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
//var data = js.Deserialize<Cliente>(dataCliente);
dynamic json = modelContato;
BomoContato contato = new BomoContato();
contato = json.BomoContato.ToObject<BomoContato>();
DBContext db = new DBContext();
db.BomoContato.Add(contato);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
示例15: Add
public async Task<IEnumerable<CvViewModel>> Add(HttpRequestMessage request)
{
var provider = new CvMultipartFormDataStreamProvider(this.workingFolder);
await request.Content.ReadAsMultipartAsync(provider);
var cvs = new List<CvViewModel>();
var db = new DBContext();
foreach(var file in provider.FileData)
{
var fileInfo = new FileInfo(file.LocalFileName);
var cv = new CV
{
Name = fileInfo.Name,
Created = fileInfo.CreationTime,
Modified = fileInfo.LastWriteTime,
Size = fileInfo.Length / 1024
};
db.CVs.Add(cv);
db.SaveChanges();
cvs.Add(new CvViewModel
{
Id = cv.Guid,
Name = fileInfo.Name,
Created = fileInfo.CreationTime,
Modified = fileInfo.LastWriteTime,
Size = fileInfo.Length /1024
});
}
return cvs;
}