C# 将Linq插入SQL时出现奇怪的错误

C# 将Linq插入SQL时出现奇怪的错误,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我在使用LINQtoSQL插入一行时遇到了一个奇怪的问题,我被要求在不使用实体框架的情况下编写一个程序,所以我正在考虑如何做到这一点,我正在遵循LINQtoSQL教程 当我尝试插入一行时,出现以下异常: System.Data.SqlServerCe.SqlCeException:无se puede修改 专栏作家。[列名=id] Linq是否正在尝试修改一列?这是我的实体,当我删除关联字段时,我可以毫无问题地保持 [Table(Name="Usuario")] public class Usua

我在使用LINQtoSQL插入一行时遇到了一个奇怪的问题,我被要求在不使用实体框架的情况下编写一个程序,所以我正在考虑如何做到这一点,我正在遵循LINQtoSQL教程

当我尝试插入一行时,出现以下异常:

System.Data.SqlServerCe.SqlCeException:无se puede修改 专栏作家。[列名=id]

Linq是否正在尝试修改一列?这是我的实体,当我删除
关联
字段时,我可以毫无问题地保持

[Table(Name="Usuario")]
public class Usuario
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert,Name="id")]
    public int id { get; set; }
    [Column]
    public string nombre { get; set; }
    [Column]
    public string direccion { get; set; }
    [Column]
    public string telefono { get; set; }
    [Column]
    public string codigoPostal { get; set; }

    private EntityRef<Ciudad> _ciudad;

    [Association (Storage="_ciudad",ThisKey="ciudad",OtherKey="id")]
    public Ciudad ciudad {
        get {return this._ciudad.Entity;}
        set { this._ciudad.Entity = value; }
    }

    private EntityRef<TipoUsuario> _tipoUsuario;

    [Association (Storage="_tipoUsuario",ThisKey="tipoUsuario",OtherKey="id")]
    public  TipoUsuario tipoUsuario {
        get {return this._tipoUsuario.Entity;}
        set { this._tipoUsuario.Entity = value; }
    }
}

“Id”列同时设置为PrimaryKey和DbGenerated。可能在插入“obj”对象之前,您正在修改导致此异常的Id值。

最后,我完成了这项工作

问题是我的存储,事实上,它导致stackoverflow异常,因为它指向自身

因此,我必须做一些更改,重命名“Usuario”表中的FK列,并将此列添加到模型中,然后设置“thiskey”和“otherkey”值,并添加“isforeignkey=true”

这是我为“usuario”设计的最终模型:

namespace项目.模型
{
[表(Name=“Usuario”)]
公共类用膳
{
[列(IsPrimaryKey=true,IsDbGenerated=true)]
公共int id{get;set;}
[专栏]
公共字符串nombre{get;set;}
[专栏]
公共字符串目录{get;set;}
[专栏]
公共字符串telefono{get;set;}
[专栏]
公共字符串{get;set;}
[专栏]
public Int32 tipoUsuarioId{get;set;}
[专栏]
public Int32 ciudadId{get;set;}
私人实体ref_ciudad;
[关联(Storage=“\u ciudad”,Name=“FK\u USUARIO\u ciudad”,OtherKey=“id”,ThisKey=“ciudadId”,IsForeignKey=true)]
公共城市{
获取{返回此。\u ciudad.Entity;}
设置{this.\u ciudad.Entity=value;}
}
私有EntityRef _tipoUsuario=新EntityRef();
[关联(Storage=“\u tipoUsuario”,Name=“FK\u USUARIO\u tipoUsuario”,OtherKey=“id”,ThisKey=“tipoUsuarioId”,IsForeignKey=true)]
公共蒂普苏亚里奥蒂普苏亚里奥{
获取{返回此。\u tipoUsuario.Entity;}
设置{this.\u tipoUsuario.Entity=value;}
}
}

感谢@aminexplo的帮助:)

您试图使用LINQ to SQL在DB中插入记录的位置在哪里?很抱歉,我更新了我的帖子并添加了我试图持久化的块。当我尝试持久化时,id的值为0,该块是我的“add”方法中唯一的代码:/那么“obj”在哪里来自?您将其传递给add方法?或者它绑定到UI元素或其他东西?并且您在表中设置了IsIdentity属性?(导致自动增量)感谢您的回答,我使用的是控制器,视图从未为id字段设置任何值。我已使用控制器类..yw的内容更新了问题。因此,根据您的业务,您应该决定是否必须自动生成id。如果在插入对象之前不应自动生成id,则必须为其设置值Id属性,否则必须在表设计模式中将此列的IsIdentity设置为“是”。
public override void agregar(Usuario obj)
{
    System.Data.Linq.Table<Usuario> usrs = context.GetTable<Usuario>();
    usrs.InsertOnSubmit(obj);

    try
    {
       context.SubmitChanges();
    }
    catch (Exception e)
    {
    }
}
public class UsuarioController : Controller
{
    UsuarioRepository dao = new UsuarioRepository();

    ...

    [HttpPost]
    public ActionResult Create(Usuario u)
    {
        //try
        //{
        dao.agregar(u);
        return RedirectToAction("Index");
        //}
        //catch
        //{
        //    return View();
        //}
    }
   ...
}
namespace Project.Models
{
    [Table(Name="Usuario")]
    public class Usuario
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int id { get; set; }
        [Column]
        public string nombre { get; set; }
        [Column]
        public string direccion { get; set; }
        [Column]
        public string telefono { get; set; }
        [Column]
        public string codigoPostal { get; set; }
        [Column]
        public Int32 tipoUsuarioId { get; set; }
        [Column]
        public Int32 ciudadId { get; set; }


        private EntityRef<Ciudad> _ciudad;
        [Association(Storage = "_ciudad", Name = "FK_USUARIO_CIUDAD", OtherKey = "id", ThisKey = "ciudadId", IsForeignKey = true)]
        public Ciudad ciudad {
            get {return this._ciudad.Entity;}
            set { this._ciudad.Entity = value; }
        }

        private EntityRef<TipoUsuario> _tipoUsuario = new EntityRef<TipoUsuario>();
        [Association (Storage="_tipoUsuario",Name="FK_USUARIO_TIPOUSUARIO",OtherKey="id",ThisKey="tipoUsuarioId",IsForeignKey=true)]
        public  TipoUsuario tipoUsuario { 
            get {return this._tipoUsuario.Entity;}
            set { this._tipoUsuario.Entity = value; }
        }

    }