1.概要
Database.SqlQuery<T> T应该是个什么样的类型呢?
1.简单类型可以吗?(int,string)
2.用类有啥要求?
2.1 简单的成员空页面? int a;
2.2 是否必须要属性 ? int a{seg;get;}
2.3 变量是否需要共有?
3.用objcect 作为这个T可以吗?
4.用dynamic可以吗
2.试验结果
通过上面的试验,可以确定 对应普通对象 只要是属性(int a{set;get;})无论是否共有都可以成功获取到数据,但是如果定义为了数据模型(DbSet<Tb1> Tb1s { set; get; }),那么数据不能少于T的内容。
3.试验代码
using MySql.Data.EntityFramework;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace EF试验2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
Program p = new Program();
p.mian();
Console.ReadKey();
}
private void mian()
{
//test1();
//test2();
//test2_2();
test2_2_1();
//test2_1();
//test3();
//test4();
//test5();
//test6();
//test6_1();
//test6_1_1();
//test7();
//test8();
//test9();
//test9_1();
//test9_1_1();
}
private void test1()
{
DBContentMy dBContentMy = new DBContentMy();
Tb1 tb1 = new Tb1();
tb1.v1 = 1;
dBContentMy.Tb1s.Add(tb1);
dBContentMy.SaveChanges();
}
private void test2()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<Tb1>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
private void test2_2()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<Tb1>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
private void test2_2_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<Tb1>("SELECT id,v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
private void test2_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<Tb1>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
/// <summary>
/// 使用非引用类型 - 可以
/// </summary>
private void test3()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<int>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
/// <summary>
/// 使用非引用类型+类型换行 - 可以
/// </summary>
private void test4()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<string>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
/// <summary>
/// 使用dynamic/object 都不行
/// 物料是但数据还是使用* 都不行
/// </summary>
private void test5()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<dynamic>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A{
int v1;
}
/// <summary>
/// 使用 少于实际变量的类型 可以
/// </summary>
private void test6()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<A>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A1
{
int v1 { set; get; }
}
/// <summary>
/// 使用 少于实际变量的类型 可以
/// </summary>
private void test6_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<A1>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A11
{
public int v1;
}
/// <summary>
/// 使用 少于实际变量的类型 可以
/// </summary>
private void test6_1_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<A11>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class B
{
int v1;
int v2;
}
/// <summary>
/// 使用 多于实际变量的类型 可以
/// </summary>
private void test7()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<B>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
/// <summary>
/// 使用 多于实际变量的类型 可以
/// </summary>
private void test8()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<B>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class C
{
int v1;
int id;
}
/// <summary>
/// 使用 多于实际变量的类型 可以
/// </summary>
private void test9()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<C>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class C1
{
int v1 { set; get; }
int id { set; get; }
}
/// <summary>
/// 使用 多于实际变量的类型 使用属性 可以
/// </summary>
private void test9_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<C1>("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
/// <summary>
/// 使用匹配的类型 使用属性 可以
/// </summary>
private void test9_1_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery<C1>("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
}
[Table("Tb1")]
public class Tb1
{
public int id { set; get; }
public int v1 { set; get; }
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class DBContentMy : DbContext
{
public DBContentMy() : base("DbConnStr")
{
Database.SetInitializer<DBContentMy>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Tb1> Tb1s { set; get; }
}
}