This program should define properties for Product model class. The program should also provide impelementations of CRUD operations for the Product model.
To achieve persistence, the data should be stored and read from file
This exercise contains project for solution code for Product entity, Product Repository and DataContext classes
namespace CRUD
{
class ProductModel
{
public int ID { get; set; }
public string Name { get; set; }
}
class ProductReposity
{
public async Task FileRead()
{
using (StreamReader reader = new StreamReader(@"TextFile.txt"))
{
string text = await reader.ReadToEndAsync();
Console.WriteLine(text);
}
}
public void Create(ProductModel model)
{
using (ProductSorage database = new ProductSorage())
{
database.ProductModel.Add(model);
database.SaveChanges();
}
}
public void Delete(int ID)
{
using (ProductSorage database = new ProductSorage())
{
var data = database.ProductModel.Where(x => x.ID == ID).FirstOrDefault();
if (data != null)
{
database.ProductModel.Remove(data);
database.SaveChanges();
}
}
}
public void Update(ProductModel productmodel)
{
using (ProductSorage database = new ProductSorage())
{
var updateProduct = database.ProductModel.Where(u => u.ID == productmodel.ID).FirstOrDefault();
updateProduct.Name = productmodel.Name;
database.SaveChanges();
}
}
public List<ProductModel> Read()
{
using (ProductSorage database = new ProductSorage())
{
try
{
return database.ProductModel.ToList();
}
catch (Exception ex)
{
throw;
}
}
}
}
class ProductSorage : DbContext
{
public ProductSorage() : base("DbConnection")
{
}
public DbSet<ProductModel> ProductModel { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
ProductReposity productReposity = new ProductReposity();
productReposity.FileRead();
Console.ReadKey();
}
}
}
Comments
Leave a comment