mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-07 11:26:41 +00:00
Fix CRUD notes
This commit is contained in:
parent
cd1fa67960
commit
8b5e725208
1 changed files with 13 additions and 90 deletions
|
@ -88,31 +88,10 @@ dotnet ef database update
|
||||||
### Create
|
### Create
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
public static bool InsertOne(Entity entity)
|
context.Add(entity);
|
||||||
{
|
context.AddRange(entities);
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
context.SaveChanges();
|
||||||
{
|
|
||||||
context.Add(entity);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool InsertMany(IEnumerable<Entity> entities)
|
|
||||||
{
|
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
|
||||||
{
|
|
||||||
context.AddRange(entities);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == entities.Count();
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Read
|
### Read
|
||||||
|
@ -120,83 +99,27 @@ public static bool InsertMany(IEnumerable<Entity> entities)
|
||||||
[Referenced Object Not Loading Fix](https://stackoverflow.com/a/5385288)
|
[Referenced Object Not Loading Fix](https://stackoverflow.com/a/5385288)
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
public static List<Entity> SelectAll()
|
context.Entities.ToList();
|
||||||
{
|
context.Entities.Find(id);
|
||||||
using(var context = new Context())
|
|
||||||
{
|
|
||||||
return context.Entities.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Entity SelectOneById(int id)
|
// force read of foreign key identifying referenced obj
|
||||||
{
|
context.Entities.Include(c => c.ForeignObject).Find(id);
|
||||||
using(var context = new Context())
|
|
||||||
{
|
|
||||||
return context.Entities.Find(id);
|
|
||||||
|
|
||||||
// force read of foreign key identifying referenced obj
|
|
||||||
return context.Entities.Include(c => c.ForeignObject).Find(id);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Update
|
### Update
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
public static bool UpdateOne(Entity entity)
|
context.Entities.Update(entity);
|
||||||
{
|
context.UpdateRange(entities);
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
context.SaveChanges();
|
||||||
{
|
|
||||||
context.Entities.Update(entity);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool UpdateMany(IEnumerable<Entity> entities)
|
|
||||||
{
|
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
|
||||||
{
|
|
||||||
context.UpdateRange(entities);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == entities.Count();
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Delete
|
### Delete
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
public static bool DeleteOne(Entity entity)
|
context.Entities.Remove(entity);
|
||||||
{
|
context.RemoveRange(entities);
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
context.SaveChanges();
|
||||||
{
|
|
||||||
context.Entities.Remove(entity);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool DeleteMany(IEnumerable<Entity> entities)
|
|
||||||
{
|
|
||||||
int rows = 0;
|
|
||||||
|
|
||||||
using(var context = new Context())
|
|
||||||
{
|
|
||||||
context.RemoveRange(entities);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return rows == entities.Count();
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue