我正在尝试在visual studio 2013中制作一个项目,但我的代码存在问题。
这是我的代码:
namespace Firma
{
public partial class Angajare : System.Web.UI.Page
{
private bool isEdit = false;
private int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
FirmeContext db = new FirmeContext();
Angajare editedAngajare = new Angajare();
if (Request.QueryString.Count == 1 && Request["id"] != null)
{
isEdit = true;
id = Convert.ToInt32(Request["id"]);
editedAngajare = db.Angajari.SingleOrDefault<Angajare>(a => a.IDAngajare == id);
if (editedAngajare != null && !IsPostBack)
txtExperienta.Text = editedAngajare.Experienta.ToString();
}
else
isEdit = false;
{
if (!IsPostBack)
{
PostList.DataSource = db.Posturi.ToList();
PostList.DataTextField = "Denumire";
PostList.DataValueField = "ID";
PostList.DataBind();
AngajatList.DataSource = db.Angajati.ToList();
AngajatList.DataTextField = "Nume";
AngajatList.DataValueField = "ID";
AngajatList.DataBind();
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
FirmeContext db = new FirmeContext();
Angajare editedAngajare;
try
{
if (isEdit)
{
editedAngajare = db.Angajari.SingleOrDefault<Angajare>(d => d.IDAngajare == id);
if (editedAngajare != null)
{
editedAngajare.Experienta = int.Parse(txtExperienta.Text);
editedAngajare.Angajat = db.Angajati.SingleOrDefault<Angajat>(n => n.ID == int.Parse(AngajatList.SelectedValue));
editedAngajare.Post = db.Posturi.SingleOrDefault<Post>(p => p.IDPost == int.Parse(PostList.SelectedValue));
db.Entry(editedAngajare).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
litMsg.Text = "Record updated!";
}
}
else
{
Angajare newan = new Angajare();
newan.Experienta = int.Parse(txtExperienta.Text);
int idAngajat=int.Parse(AngajatList.SelectedValue);
newan.Angajat = db.Angajati.SingleOrDefault<Angajat>(n => n.ID == idAngajat);
int idPost = int.Parse(PostList.SelectedValue);
newan.Post = db.Posturi.SingleOrDefault<Post>(p => p.IDPost == idPost);
db.Angajari.Add(newan);
db.Entry(newan).State = System.Data.Entity.EntityState.Added;
db.SaveChanges();
litMsg.Text = "New Record Added!";
}
}
catch (Exception ex)
{
litMsg.Text = "Error: " + ex.Message;
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Server.Transfer("Default.aspx");
}
}
}
我有很多错误。例如,在这一行
editedAngajare = db.Angajari.SingleOrDefault<Angajare>(d => d.IDAngajare == id);
它说:
"Error 10 'Firma.Angajare' does not contain a definition for 'IDAngajare' and no extension method 'IDAngajare' accepting a first argument of type 'Firma.Angajare' could be found (are you missing a using directive or an assembly reference?)"
Experienta,Angajat,Post
同样的事情 editedAngajare.Experienta = int.Parse(txtExperienta.Text);
editedAngajare.Angajat = db.Angajati.SingleOrDefault<Angajat>(n => n.ID == int.Parse(AngajatList.SelectedValue));
editedAngajare.Post = db.Posturi.SingleOrDefault<Post>(p => p.IDPost == int.Parse(PostList.SelectedValue));
db.Entry(editedAngajare).State = System.Data.Entity.EntityState.Modified;
我还会告诉你我的数据库:
public class Angajare
{
[Key]
public int IDAngajare { get; set; }
public int Experienta { get; set; }
public virtual Post Post { get; set; }
public virtual Angajat Angajat { get; set; }
}
public class Angajat
{
[Key]
public int ID { get; set; }
public string Nume { get; set; }
public string Prenume { get; set; }
public DateTime DataAngajare { get; set; }
public virtual ICollection<Angajare> Angajari { get; set; }
}
public class Post
{
[Key]
public int IDPost { get; set; }
public string Denumire { get; set; }
public int ExperientaNecesara { get; set; }
public virtual ICollection<Angajare> Angajari { get; set; }
}
请帮助我,因为我不知道出了什么问题。 谢谢你的建议。
答案 0 :(得分:0)
您似乎有两个班级Angajare
。一个是Web.UI.Page,一个是数据库模型。尝试在linq查询中指定命名空间。