I am using Visual Studio 2012 Express for Web and I am trying to create ASP MVC 4 Project. I had create some data model classes, generate the view and all works fine.
// Example model classes
class MyData
{
public int ID { get; set; }
public string SomeData { get; set; }
}
class MyDataDbContext : DbContext
{
public DbSet<MyData> Data { get; set; }
}
But I am trying to figure out how it is possible that Data property from MyDataDbContext class has automatickly correct data?
On msdn is written that DbSet is not publicly constructible and can only be created from a DbContext instance. So HOW it is possible that DbContext can create instance in child class?
When I pressed F12 over DbSet class I read summary like this:
A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext
using the DbContext.Set method.
So I can't understand how DbSet class of SomeData property uses DbContext.Set method incide MyDataDbContext class?
In other words... After I create instance of MyDataDbContext class the Data property is somehow automatikcly filled...
How it is possible?
Like we see above MyDataDbContext class do not initialize the Data property.
PS - I was using
this tutorial to lear how to create ASP.NET MVC 4 Project.
that Data property from MyDataDbContext class has automatickly correct data?
Hello,
In fact I think this can been found through the reflector——
【Reflected codes for DbSet<T>】
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification="Name is intentional"), SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification="Casing is intentional")]
public class DbSet<TEntity> : DbQuery<TEntity>, IDbSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable, IInternalSetAdapter where TEntity: class
{
// Fields
private readonly InternalSet<TEntity> _internalSet;
// Methods
internal DbSet(InternalSet<TEntity> internalSet) : base(internalSet)
{
this._internalSet = internalSet;
}
public TEntity Add(TEntity entity)
{
RuntimeFailureMethods.Requires(entity != null, null, "entity != null");
this._internalSet.Add(entity);
return entity;
}
public TEntity Attach(TEntity entity)
{
RuntimeFailureMethods.Requires(entity != null, null, "entity != null");
this._internalSet.Attach(entity);
return entity;
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity: class, TEntity
{
return this._internalSet.Create(typeof(TDerivedEntity));
}
public TEntity Create()
{
return this._internalSet.Create();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public TEntity Find(params object[] keyValues)
{
return this._internalSet.Find(keyValues);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode()
{
return base.GetHashCode();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public Type GetType()
{
return base.GetType();
}
[SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates", Justification="Intentionally just implicit to reduce API clutter.")]
public static implicit operator DbSet(DbSet<TEntity> entry)
{
RuntimeFailureMethods.Requires(entry != null, null, "entry != null");
return (DbSet) entry._internalSet.InternalContext.Set(entry._internalSet.ElementType);
}
public TEntity Remove(TEntity entity)
{
RuntimeFailureMethods.Requires(entity != null, null, "entity != null");
this._internalSet.Remove(entity);
return entity;
}
public DbSqlQuery<TEntity> SqlQuery(string sql, params object[] parameters)
{
RuntimeFailureMethods.Requires(!string.IsNullOrWhiteSpace(sql), null, "!string.IsNullOrWhiteSpace(sql)");
RuntimeFailureMethods.Requires(parameters != null, null, "parameters != null");
return new DbSqlQuery<TEntity>(new InternalSqlSetQuery(this._internalSet, sql, false, parameters));
}
// Properties
public ObservableCollection<TEntity> Local
{
get
{
return this._internalSet.Local;
}
}
IInternalSet IInternalSetAdapter.InternalSet
{
get
{
return this._internalSet;
}
}
}
From the above codes you should know tht it has only offered you an internal constructor,which means it can be ONLY initialized in the same namespace of EntityFramework instead of outside.
【DbContext】
DbContext is usually used with a derived type that contains
DbSet<> properties for the root entities of the model. These sets are automatically initialized when the instance of the derived class is created. This behavior can be modified by applying the
SuppressDbSetInitializationAttribute attribute to either the entire derived context class, or to individual properties on the class.
RedQueen87
0 Points
1 Post
How DbSet and DbContext classes cooperate?
Nov 24, 2012 09:05 AM|LINK
I am using Visual Studio 2012 Express for Web and I am trying to create ASP MVC 4 Project.
I had create some data model classes, generate the view and all works fine.
// Example model classes class MyData { public int ID { get; set; } public string SomeData { get; set; } } class MyDataDbContext : DbContext { public DbSet<MyData> Data { get; set; } }But I am trying to figure out how it is possible that Data property from MyDataDbContext class has automatickly correct data?
On msdn is written that DbSet is not publicly constructible and can only be created from a DbContext instance.
So HOW it is possible that DbContext can create instance in child class?
When I pressed F12 over DbSet class I read summary like this:
A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext.Set method.
So I can't understand how DbSet class of SomeData property uses DbContext.Set method incide MyDataDbContext class?
In other words... After I create instance of MyDataDbContext class the Data property is somehow automatikcly filled... How it is possible?
Like we see above MyDataDbContext class do not initialize the Data property.
PS - I was using this tutorial to lear how to create ASP.NET MVC 4 Project.
eric2820
Contributor
2777 Points
1161 Posts
Re: How DbSet and DbContext classes cooperate?
Nov 24, 2012 09:11 PM|LINK
A DbSet<type> is just a wrapper aournd one of your Entity classes.
The DbSet component simply adds some database specific methods (mostly private) to your Entity classes.
You include System.Linq into your Controller classes and use Linq along with an instance of your EFDbContext class.
Your connection string must be the same as your EFDbContext class name for EF to find it.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How DbSet and DbContext classes cooperate?
Nov 25, 2012 01:22 AM|LINK
Hello,
In fact I think this can been found through the reflector——
【Reflected codes for DbSet<T>】
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification="Name is intentional"), SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification="Casing is intentional")] public class DbSet<TEntity> : DbQuery<TEntity>, IDbSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable, IInternalSetAdapter where TEntity: class { // Fields private readonly InternalSet<TEntity> _internalSet; // Methods internal DbSet(InternalSet<TEntity> internalSet) : base(internalSet) { this._internalSet = internalSet; } public TEntity Add(TEntity entity) { RuntimeFailureMethods.Requires(entity != null, null, "entity != null"); this._internalSet.Add(entity); return entity; } public TEntity Attach(TEntity entity) { RuntimeFailureMethods.Requires(entity != null, null, "entity != null"); this._internalSet.Attach(entity); return entity; } public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity: class, TEntity { return this._internalSet.Create(typeof(TDerivedEntity)); } public TEntity Create() { return this._internalSet.Create(); } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } public TEntity Find(params object[] keyValues) { return this._internalSet.Find(keyValues); } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return base.GetHashCode(); } [EditorBrowsable(EditorBrowsableState.Never)] public Type GetType() { return base.GetType(); } [SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates", Justification="Intentionally just implicit to reduce API clutter.")] public static implicit operator DbSet(DbSet<TEntity> entry) { RuntimeFailureMethods.Requires(entry != null, null, "entry != null"); return (DbSet) entry._internalSet.InternalContext.Set(entry._internalSet.ElementType); } public TEntity Remove(TEntity entity) { RuntimeFailureMethods.Requires(entity != null, null, "entity != null"); this._internalSet.Remove(entity); return entity; } public DbSqlQuery<TEntity> SqlQuery(string sql, params object[] parameters) { RuntimeFailureMethods.Requires(!string.IsNullOrWhiteSpace(sql), null, "!string.IsNullOrWhiteSpace(sql)"); RuntimeFailureMethods.Requires(parameters != null, null, "parameters != null"); return new DbSqlQuery<TEntity>(new InternalSqlSetQuery(this._internalSet, sql, false, parameters)); } // Properties public ObservableCollection<TEntity> Local { get { return this._internalSet.Local; } } IInternalSet IInternalSetAdapter.InternalSet { get { return this._internalSet; } } }From the above codes you should know tht it has only offered you an internal constructor,which means it can be ONLY initialized in the same namespace of EntityFramework instead of outside.
【DbContext】
DbContext is usually used with a derived type that contains DbSet<> properties for the root entities of the model. These sets are automatically initialized when the instance of the derived class is created. This behavior can be modified by applying the SuppressDbSetInitializationAttribute attribute to either the entire derived context class, or to individual properties on the class.