Big_w_powah
Trakanon Raider
- 1,887
- 750
namespace SchoolDataLayer
{
public class Context: DbContext
{
public SchoolDBContext() : base("name=NAMEOFCONNECTIONSTRING")
{
}
}
}
<connectionStrings>
<add name="NAMEOFCONNECTIONSTRING"
connectionString="Data Source=.;Initial Catalog=DATABASENAME;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
using System.Data.Entity;
namespace MachineReport.DataContext
{
public class ReportContext : DbContext
{
public ReportContext() : base("name=MyConnectionString")
{
}
}
}
public abstract class Entity : IObjectState
{
#region Public Properties
[NotMapped]
public ObjectState ObjectState
{
get;
set;
}
#endregion
}
public interface IObjectState
{
#region Public Properties
[NotMapped]
ObjectState ObjectState
{
get;
set;
}
#endregion
}
public enum ObjectState
{
Unchanged,
Added,
Modified,
Deleted
}
namespace MachineReport.Models
{
[Table("Salaries")]
public class Salary :Entity
{
/// <summary>
/// Main Key, by using conventions, EF will understand that this is an identity column, an ever increasing integer.
/// </summary>
public int SalaryId
{
get; set;
}
/// <summary>
/// Required Database field with ammount
/// </summary>
[Required]
public decimal Amount
{
get; set;
}
[MaxLength(300)]
public string FixedLenth
{
get; set;
}
public string VeryLongString
{
get; set;
}
/// <summary>
/// Required Database fieled with date of effectiveness
/// </summary>
[Required]
public DateTime EffectiveOn
{
get; set;
}
[Required]
public bool Inactive
{
get; set;
}
public bool? Deleted
{
get; set;
}
}
}
OMG is private messaging broke?
I definitely will, in a similar situation at work.If this is a big issue for people, I'm sure we can take it to PMs, but this is basically a tutorial and may be useful to others.
public class ReportContext : DbContext
{
public ReportContext() : base("name=ReportConnectionString")
{
// this forces you to generate the migrations files manually. Its better because you will learn how it affects teh database
Configuration.AutoDetectChangesEnabled = false;
// This disables Lazy loading. LAzy laoding its good for simplicity, but bad for database performance.
Configuration.LazyLoadingEnabled = false;
// When lazy is on, proxy are created to create the entities. since we dont have laze, we turn it off
Configuration.ProxyCreationEnabled = false;
// DAtabase timeout
Database.CommandTimeout = 300;
}
#region Db sets or tables
public DbSet<Salary> Salaries
{
get; set;
}
#endregion
}