Iam New In Entity-framework...i created Windows Apllication in visual studio 2019
iam using Entity-framework 6.i created DatabaseContext like that
using LedgerGroup.TheLedger.Core.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LedgerGroup.TheLedger.Core.Data
{
public class DatabaseContext: DbContext
{
public DatabaseContext() : base("DatabaseConnectionString")
{
Database.SetInitializer<databasecontext>(new CreateDatabaseIfNotExists<databasecontext>());
}
public DbSet<customer> Customers { get; set; }
}
}
and then created Customer Table like that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LedgerGroup.TheLedger.Core.Entities
{
public class Customer:BaseEntity
{
public string Name { get; set; }
public string Telephone { get; set; }
public string Address { get; set; }
public string CarType { get; set; }
public string CarModel { get; set; }
public string Notes { get; set; }
}
}
and then i create BaseEntity Class which Every Table in database will get somecolumns(Id ,CreatedBy,LastUpdatedBy,CreatedOn,LastUpdatedOn,bool Deleted)
from this class
this the code inside BaseEntity class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LedgerGroup.TheLedger.Core.Entities
{
public class BaseEntity
{
public int Id { get; set; }
public int CreatedBy { get; set; }
public int LastUpdatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime LastUpdatedOn { get; set; }
public bool Deleted { get; set; }
}
}
and i add-migration CreateTheLedgerDB
which create the databse in my databse Engine and Update-Databse
then i wanted to insert Some records inside customers Table
i made Aform called it AddCustomerForm and in the event BtnAddCustomer_Click(object sender, EventArgs e) i used some code to insert data
but it gives me this error
(SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.)>
can Any one help me
in the AddCustomerForm i want to Insert new customer
What I have tried:
private void BtnAddCustomer_Click(object sender, EventArgs e)
{
using (var context = new DatabaseContext())
{
var customer = new Customer()
{
Name = txtCustomerName.Text,
Telephone = txtCustomerPhone.Text,
Address = txtCustomerAddress.Text,
CarType = txtCustomerCar.Text,
CarModel = txtCustomerCarModel.Text
};
context.Customers.Add(customer);
context.SaveChanges();
MessageBox.Show("Saved Successfully");
}
}