教学资料管理系统演示,仿照视频链接在最下方
第二天 解决EF框架在不同项目中引用问题
2.1 引用导在运行期间才报错
图2.1.1
写到获取用户名后,点击运行会提示下方异常
图2.1.2
提示在这段代码所在项目里的app.config 里面没找到连接字符串,怎么处理呢,去其他引用的项目app。config找,我从command中引用的,但是所以去models找app.config,
图2.1.3
把这个复制到需要的app.config里面
图2.1.4 再次运行
图2.1.5 刚运行又提示一个异常
未处理System.Data.Entity.Core.MetadataException
HResult=-2146232007
Message=Schema specified is not valid. Errors:
Model1.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SqlClient’. Make sure the provider is registered in the ‘entityFramework’ section of the application config file.http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
处理方法
图2.1.6 把整个config内容都复制进去
再次出现异常
图2.1.7 就是找不到System.Data.SqlClient’ 看看为什么找不到
The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
图2.1.8 安装个包看看
图2.1.9 安装不上,后来发现应该安装sysytem.sql.client
图2.1.10 右击项目,查看属性 框架太低,安装一个低版本的包试试看
图2.1.11 还是不行??放弃挣扎,框架改成4.7.2高版本,改完之后先看看能不能运行,有可能无法运行了,后来发现应该安装sysytem.sql.client,还是无法运行,报错内容和2.1.7一样,再仔细看内容:‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded.
图2.1.12 其中有一个是这个,做什么不知道
图2.1.13 查到最后没有加EF包
图2.1.14 终于提示下一步了
2.2 添加一行数据库内容
图2.2.1 在Teacher库里面添加上内容
图2.2.2 输入刚才的账号密码
图2.2.3 成功登录进去
2.3 添加自定义画面
2.3.1 添加试试看有什么区别
图2.3.2 继承UserControl,这个都有什么
图2.3.3 正常的一些都有,再继续看ContainControl里面有什么,一个个看,越看东西越高级,基础人用不上
图2.3.4 先加两个label 写上文字,接着研究怎么赋值
图2.3.5 回到主窗体,加一个panel,不知道怎么用,扔一个button进去看看
图2.3.6 通过controls.ADD方法 会把东西加到panel里面,随着框一起移动
图2.3.7 会显示 IndexView上画面内容
图2.3.8 什么也看不见,说明画面范围显示不全,浏览器搜索一下 Winform UserControl怎么随着大小变动,找Dock属性,但是右键画面属性里看不到,这个时候就用代码写上Dock,fill。
图2.3.9 通过代码修改Dock
图2.3.10 为了便于调试,直接给好账号密码
图2.3.11 已经和窗口绑定一起了,所有画面做一个统一的大小800,400
2.4 对主画面进行填充
图2.4.1 menu是最上面的菜单,tool暂时未知,status是在最下面,panel中间不知道干什么的
图2.4.2 做下个界面跳转和当前界面清除
图2.4.3
图2.4.4 左侧是添加信息,右侧是显示添加后效果
图2.4.5 combox下拉选项直接输入进去就可以进行选择了
private void buttonlogin_Click(object sender, EventArgs e)
{
ClassService service = new ClassService();
Class c1 = new Class();
c1.Name = textBoxName.Text.Trim();
c1.Grade = comboBoxGrade.Text.Trim();
c1.ClassNumber = comboBoxClass.Text.Trim();
c1.InsertDate = DateTime.Now;
if (string.IsNullOrEmpty(c1.Name))
{
MessageBox.Show("名字不能为空");
}
int count = service.Insert(c1);
if (count>0)
{
MessageBox.Show("成功");
//把查出来的值绑定给这个控件
dataGridView1.DataSource = service.Select();
//这么写完有问题,无论写不写内容都只提示成功,并且把空数据传过去
}
else
{
MessageBox.Show("失败");
}
}
F12 insert
public int Insert(Class t)
{
//还要判断充不重复
var old = db.Class.ToList().FirstOrDefault(o => o.Name == t.Name && o.Grade == t.Grade && o.ClassNumber == t.ClassNumber);
if (old!=null)
{
return 0;
}
db.Entry(t).State = System.Data.Entity.EntityState.Added;
return db.SaveChanges();
// throw new NotImplementedException();
}
图2.4.6 就算删除了ID也是继续累计,各种问题都在出现