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 ...
随机推荐
- lftp下载文件无法覆盖,提示" file already existst and xfer:clobber is unset" 问题解决
在 /etc/lftp.conf 文件中添加以下配置即可 set xfer:clobber on
- RDD的cache 与 checkpoint 的区别
问题:cache 与 checkpoint 的区别? 关于这个问题,Tathagata Das 有一段回答: There is a significant difference between cac ...
- delphi 中Adoquery ,在打开时能否让记录指针不移动? [问题点数:40分,结帖人microd]
delphi 中Adoquery ,在打开时能否让记录指针不移动?由于数据集Adoquery 时,它的针指称动会废时,能否在打开完成之前不让记录指针不移动.打开完毕之后再回复移动? 这样用:self. ...
- redis 3.2.5单机版安装、使用、systemctl管理Redis启动、停止、开机启动
参照地址 http://www.mamicode.com/info-detail-1488639.html 前提:防火墙安装,然后打开端口,设置开机启动 一.redis源码安装 [root@host- ...
- [Http] Difference between POST and GET?
What is the difference between POST and GET HTTP requests? GET and POST are two different types of H ...
- ajax将数组或list集合传到后台 的 【坑】
代码如下 function deleteChecked() { var orderNosList = new Array(); var rows = $("#dataGrid"). ...
- Spring Boot开发Web应用之Thymeleaf篇
前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot提供了spring-boot-starter-web为Web开发予以 ...
- Chrome浏览器界面截图
常常出现这么一个场景: 网页比较长,需要滚动屏幕才能看完整.这时候如需截屏,则比较麻烦. 如下为解决方法: 推荐Chrome浏览器: 按F12打开调试页面,同时按下ctrl + shift + p, ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- 【VS开发】recv函数函数返回值说明(特别有利于工程调试)
recv函数 int recv( SOCKET s, char FAR *buf, int len, int flags); 不论是客户还是服务器应用程序都用recv函数从TCP连接的另一端接收数据. ...