1.概要
Database.SqlQuery
1.简单类型可以吗?(int,string)
2.用类有啥要求?
2.1 简单的成员空页面? int a;
2.2 是否必须要属性 ? int a{seg;get;}
2.3 变量是否需要共有?
3.用objcect 作为这个T可以吗?
4.用dynamic可以吗
2.试验结果
T(Model) | ||||||
分类 | 成员/属性 | 数据多T | 数据少于T | 共有 | 私有 | |
简单类型 | 1 | OK | ||||
object | NG | |||||
dynamic | NG | |||||
普通对象 | int a; | 1 | 1 | NG | ||
1 | 1 | NG | ||||
int a{set;get;} | 1 | 1 | OK | |||
1 | OK | |||||
1 | 1 | OK | ||||
1 | OK | |||||
数据模型 | int a{set;get;} | 1 | OK | |||
1 | NG | |||||
数据可以少于T单是主键不能少 |
通过上面的试验,可以确定 对应普通对象 只要是属性(int a{set;get;})无论是否共有都可以成功获取到数据,但是如果定义为了数据模型(DbSet
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("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("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("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("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
///
/// 使用非引用类型 - 可以
///
private void test3()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
///
/// 使用非引用类型+类型换行 - 可以
///
private void test4()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
//Console.WriteLine();
}
///
/// 使用dynamic/object 都不行
/// 物料是但数据还是使用* 都不行
///
private void test5()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A{
int v1;
}
///
/// 使用 少于实际变量的类型 可以
///
private void test6()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A1
{
int v1 { set; get; }
}
///
/// 使用 少于实际变量的类型 可以
///
private void test6_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class A11
{
public int v1;
}
///
/// 使用 少于实际变量的类型 可以
///
private void test6_1_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class B
{
int v1;
int v2;
}
///
/// 使用 多于实际变量的类型 可以
///
private void test7()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT * FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
///
/// 使用 多于实际变量的类型 可以
///
private void test8()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class C
{
int v1;
int id;
}
///
/// 使用 多于实际变量的类型 可以
///
private void test9()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
class C1
{
int v1 { set; get; }
int id { set; get; }
}
///
/// 使用 多于实际变量的类型 使用属性 可以
///
private void test9_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("SELECT v1 FROM tb1").FirstOrDefaultAsync();
a.Wait();
var b = a.Result;
Console.WriteLine(b);
}
///
/// 使用匹配的类型 使用属性 可以
///
private void test9_1_1()
{
DBContentMy dBContentMy = new DBContentMy();
var a = dBContentMy.Database.SqlQuery("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(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet Tb1s { set; get; }
}
}