添加方法:

public int Insert<T>(T model)
where T : class, new()
{
int sucess = 0;
if (model is TempInfo)
{
TempInfo temp = (TempInfo)(object)model;
sucess = InsertUser(temp);
}
if (model is StudentInfo)
{
StudentInfo student = (StudentInfo)(object)model;
sucess = InsertStudent(student);
}
return sucess;
}

调用添加数据方法:

int sucess = 0;
EFEntity entity = new EFEntity();
InsertOprater oprator = new InsertOprater();
TempInfo u = new TempInfo();
u.Name = "zhang.san3";
u.Age = 21;
u.Address = "沈阳市3";
u.Phone = "13066668888";
u.condition = "condition3";
sucess = oprator.Insert<TempInfo>(u);
StudentInfo student = new StudentInfo();
student.StudentName = "通过模型添加数据3";
student.Grades_GradeId = 26;
sucess = oprator.Insert<StudentInfo>(student);
if (sucess > 0)
{
MessageBox.Show("添加数据模块成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加数据模块失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private int InsertUser(TempInfo temp)
{
int sucess = 0;
using (EFEntity ef = new EFEntity())
{
ef.TempInfo.Add(temp);
sucess = ef.SaveChanges();
}
return sucess;
}
private int InsertStudent(StudentInfo student)
{
int sucess = 0;
using (EFEntity ef = new EFEntity())
{
ef.StudentInfo.Add(student);
sucess = ef.SaveChanges();
}
return sucess;
}

Temp类

[Table("TempInfo", Schema = "MyData")]
public class Temp
{
[Key]
public int Id { get; set; }
[MaxLength(20)]
public string Name { get; set; }
public int Age { get; set; }
[MaxLength(30)]
public string Phone { get; set; }
[MaxLength(50)]
public string Address { get; set; }
[MaxLength(200)]
public string condition { get; set; }
}

以下是引用生成表的类名

public class MyContext:DbContext
{
public MyContext():base()
{

}
/*
public DbSet<User> User { get; set; }
public DbSet<Temp> Temp { get; set; }
public DbSet<Student> Student { get; set; }
public DbSet<StudentAddress> StudentAddress { get; set; }
public DbSet<Grade> Grade { get; set; }
public DbSet<Course> Course { get; set; }
**/

public DbSet<UserInfo> UserInfo { get; set; }
public DbSet<TempInfo> TempInfo { get; set; }
public DbSet<StudentInfo> StudentInfo { get; set; }
public DbSet<StudentAddressInfo> StudentAddressInfo { get; set; }
public DbSet<GradeInfo> GradeInfo { get; set; }
public DbSet<CourseInfo> CourseInfo { get; set; }
}

c#传入类名添加类对应的表数据的更多相关文章

  1. form 向java控制类 提交多表数据 、提交list数组数据

    案例:form中有三个表的数据,一个主表,两个子表 1.在主表model类添加 对应子表数据集 2.界面上主表定义 3.控制类接收,直接用主表对象接收即可

  2. 01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类

    1==>控制字体图标的大小用 font-size:16px; <i class="el-icon-arrow-left right-show-aside-icon"&g ...

  3. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  4. Jquery属性操作、添加类

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order

    如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...

  6. Spring中添加新的配置表,并对新的配置表进行处理

    实习过程中boss交代的任务(以下出现的代码以及数据只给出小部分,提供一个思路) 目的:Spring中添加新的配置表,并对新的配置表进行处理:替换的新的配置表要友好,同时保证替换前后功能不能发生变化. ...

  7. Python小白学习之如何添加类属性和类方法,修改类私有属性

    如何添加类属性和类方法,修改类私有属性 2018-10-26  11:42:24 类属性.定义类方法.类实例化.属性初始化.self参数.类的私有变量的个人学习笔记 直接上实例: class play ...

  8. Hibernate每个层次类一张表(使用注释)

    在上一文章中,我们使用xml文件将继承层次映射到一个表. 在这里,我们将使用注释来执行同样的任务.需要使用@Inheritance(strategy = InheritanceType.SINGLE_ ...

  9. Hibernate每个具体类一张表映射(使用注释)

    在每个类创建一张表的情况下, 表中不使用Null值的列. 这种方法的缺点是在子类表中创建了重复的列. 在这里,我们需要在父类中使用@Inheritance(strategy = Inheritance ...

随机推荐

  1. PHP格式化数字和SMARTY格式化数字的方法

    PHP格式化: $num="3";$format="%06d";  //6是位数,这里有6位数,0是不足6位的补0$a=sprintf($format,$num ...

  2. 014-数据结构-树形结构-基数树、Patricia树、默克尔树、梅克尔帕特里夏树( Merkle Patricia Tree, MPT)

    一.基数树 Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构.与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩.同样的,Radi ...

  3. js 生成树以及关键字搜索生成树

    function main(keywords,data){ function fn(arr){ var flag = false; for(var i = 0;i <arr.length;i++ ...

  4. Linux 查看磁盘空间 相关命令

    Linux 查看磁盘空间 相关命令 实际工作中,我们经常需要查看磁盘空间的使用情况,以防止磁盘空间不足,导致的系统崩溃或者服务异常等问题. 常用的磁盘空间查看命令如下: 1.查看磁盘空间的整体使用情况 ...

  5. 改进后的socket轮子,欢迎挑战

    运行环境.net core2.1 下载地址

  6. redis集群 应该注意的问题

    redis cluster注意的问题 : ‘cluster-require-full-coverage’参数的设置.该参数是redis配置文件中cluster模式的一个参数,从字面上基本就能看出它的作 ...

  7. 转:获取windows凭证管理器明文密码

    1.运行cmdkey /list查看windows保存凭证 方法1.mimikaz mimikatz vault::cred 2.利用powershell尝试获取 windows 普通凭据类型中的明文 ...

  8. PTA --- L2-003 月饼

    这道题挺简单的,测试点2没过的话,注意题目中是两个正整数,而其余是正数 [没注意到,,,踩坑了,,,] 题目地址: https://pintia.cn/problem-sets/99480504638 ...

  9. QT下过多点的曲线绘制

    绘制过多点的曲线意义重大.但通过试验,QT的PainterPath不是很如意.当多段曲线围成一个区域时,PainterPath内并不包含该区域的所有面积,只包含曲线和其弦构成的面积. 为了解决这一问题 ...

  10. NDK学习笔记-JNI开发流程

    JNI(Java Native Interface)Java本地化接口,Java调用C/C++,C/C++调用Java的一套API接口 实现步骤 在Java源文件中编写native方法 public ...