c#传入类名添加类对应的表数据
添加方法:
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#传入类名添加类对应的表数据的更多相关文章
- form 向java控制类 提交多表数据 、提交list数组数据
案例:form中有三个表的数据,一个主表,两个子表 1.在主表model类添加 对应子表数据集 2.界面上主表定义 3.控制类接收,直接用主表对象接收即可
- 01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类
1==>控制字体图标的大小用 font-size:16px; <i class="el-icon-arrow-left right-show-aside-icon"&g ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- Jquery属性操作、添加类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order
如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...
- Spring中添加新的配置表,并对新的配置表进行处理
实习过程中boss交代的任务(以下出现的代码以及数据只给出小部分,提供一个思路) 目的:Spring中添加新的配置表,并对新的配置表进行处理:替换的新的配置表要友好,同时保证替换前后功能不能发生变化. ...
- Python小白学习之如何添加类属性和类方法,修改类私有属性
如何添加类属性和类方法,修改类私有属性 2018-10-26 11:42:24 类属性.定义类方法.类实例化.属性初始化.self参数.类的私有变量的个人学习笔记 直接上实例: class play ...
- Hibernate每个层次类一张表(使用注释)
在上一文章中,我们使用xml文件将继承层次映射到一个表. 在这里,我们将使用注释来执行同样的任务.需要使用@Inheritance(strategy = InheritanceType.SINGLE_ ...
- Hibernate每个具体类一张表映射(使用注释)
在每个类创建一张表的情况下, 表中不使用Null值的列. 这种方法的缺点是在子类表中创建了重复的列. 在这里,我们需要在父类中使用@Inheritance(strategy = Inheritance ...
随机推荐
- zabbix通过SDK和API获取阿里云RDS的监控数据
阿里云的RDS自带的监控系统获取数据不怎么直观,想要通过API获取数据通过zabbix显示,因为网上资料缺乏和其他一些原因,获取API签名很困难,但使用阿里云的SDK可以完美避开获取签名的步骤. 阿里 ...
- centos6 安装tensorflow
1.升级python2.6.6 至 python2.7.12+ 升级时./configure --prefix=/usr/local/python27 --enable-unicode=ucs4 2. ...
- java数据结构之LinkedList
一.LinkedList源码注释 //LinkedList源码 jdk版本1.8.0_121 public class LinkedList<E> extends AbstractSequ ...
- swagger-注解
常用注解 @Api(value = "xxx"):用于类,表示标识这个类是swagger的资源. tags–表示说明,如果有多个值,会生成多个list value–也是说明,可以使 ...
- Linux(CentOS)下安装tesseract-ocr以及配置依赖leptonica
下载 wget https://github.com/tesseract-ocr/tesseract/archive/4.1.0.tar.gz wget http://www.leptonica.or ...
- 【并行计算-CUDA开发】从熟悉到精通 英伟达显卡选购指南
举报 说到显卡,就不免令人想到英伟达和AMD两家面向个人消费级和企业级最大的显示芯片生产企业,英伟达和AMD,今天小编为大家简单的介绍一下英伟达的显卡选购方面的攻略,为一些想要购买显卡的用户提供一些参 ...
- python3正则表达式详细用法示例
转载自:https://www.runoob.com/python3/python3-reg-expressions.html
- POJ2411 Mondriaan's Dream 【状压dp】
没错,这道题又是我从LZL里的博客里剽过来的,他的题真不错,真香. 题目链接:http://poj.org/problem?id=2411 题目大意:给一个n * m的矩形, 要求用 1 * 2的小方 ...
- python列表的切片与复制
切片,即处理一个完整列表中部分数据. 语法 变量[起始索引:终止索引:步长] 首先创建一个字符串列表 >>> cars = ['toyota', 'honda', 'mazda', ...
- 脚本(bat、shell)调用conda
官网说明:https://docs.conda.io/projects/conda/en/latest/user-guide/troubleshooting.html#using-conda-in-w ...