This solution code should contain definition for product model along with it’s properties. The program should also provide impelementations of CRUD operations for the Product model
This exercise contains project for solution code for Product entity and Product Repository classes
class ProductModel
{
public int ID { get; set; }
public string Name { get; set; }
}
class ProductReposity
{
public async Task FileRead()
{
using (StreamReader reader = new StreamReader(@"YourFile.txt"))
{
string text = await reader.ReadToEndAsync();
Console.WriteLine(text);
}
}
public void Create(ProductModel productModel)
{
using (ProductStorage database = new ProductStorage())
{
database.ProductModel.Add(productModel);
database.SaveChanges();
}
}
public void Delete(int ID)
{
using (ProductStorage database = new ProductStorage())
{
var data = database.ProductModel.Where(p => p.ID == ID).FirstOrDefault();
if (data != null)
{
database.ProductModel.Remove(data);
database.SaveChanges();
}
}
}
public void Update(ProductModel productmodel)
{
using (ProductStorage database = new ProductStorage())
{
var updateProduct = database.ProductModel.Where(p => p.ID == productmodel.ID).FirstOrDefault();
updateProduct.Name = productmodel.Name;
database.SaveChanges();
}
}
public List<ProductModel> Read()
{
using (ProductStorage database = new ProductStorage())
{
try
{
return database.ProductModel.ToList();
}
catch (Exception ex)
{
throw;
}
}
}
}
class ProductStorage : DbContext
{
public ProductStorage() : base("YourDataBaseConnection")
{
}
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