简体   繁体   English

使用Linq插入SQL的奇怪错误

[英]Weird error Inserting With Linq to SQL

I'm having a strange problem inserting a row using Linq To SQL, I'm being asked to write a program without use Entity Framework, so I'm figuring how to make this and I'm following a Linq to SQL tutorial. 我在使用Linq To SQL插入行时遇到一个奇怪的问题,被要求编写一个不使用Entity Framework的程序,因此我正在弄清楚如何做到这一点,并且正在关注Linq to SQL教程。

When I try to insert a row I get this exception: 当我尝试插入一行时,出现此异常:

System.Data.SqlServerCe.SqlCeException: No se puede modificar la columna. System.Data.SqlServerCe.SqlCeException:没有列的修改。 [ Column name = id ] [列名称= id]

Is Linq is trying to modify a column?, this is my entity, when I remove the Association field I'm able to persist without problem. Linq是否要尝试修改列?,这是我的实体,当我删除“ Association字段时,我可以毫无问题地坚持下去。

[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; }
    }
}

Update: 更新:

Here is when I'm trying to persist: 这是我要坚持的时候:

public override void agregar(Usuario obj)
{
    System.Data.Linq.Table<Usuario> usrs = context.GetTable<Usuario>();
    usrs.InsertOnSubmit(obj);

    try
    {
       context.SubmitChanges();
    }
    catch (Exception e)
    {
    }
}

And this is my controller : 这是我的控制器:

public class UsuarioController : Controller
{
    UsuarioRepository dao = new UsuarioRepository();

    ...

    [HttpPost]
    public ActionResult Create(Usuario u)
    {
        //try
        //{
        dao.agregar(u);
        return RedirectToAction("Index");
        //}
        //catch
        //{
        //    return View();
        //}
    }
   ...
}

The "Id" column is both set to PrimaryKey and DbGenerated. “ Id”列均设置为PrimaryKey和DbGenerated。 Probably before inserting the "obj" object somehow you are modifying the value of Id which causes this exception. 可能在以某种方式插入“ obj”对象之前,您正在修改导致此异常的Id的​​值。

Finally I made this work 最后我做了这项工作

The problem was my Storage, in fact, it causes a stackoverflow exception because is pointing to itself. 问题出在我的存储上,实际上,由于指向自身,它会导致stackoverflow异常。

So I had to make some changes, renaming my FK columns on "Usuario" table and adding this column to my model, then set the "thiskey" and "otherkey" values and adding "isforeignkey=true". 因此,我必须进行一些更改,在“ Usuario”表上重命名我的FK列,并将此列添加到我的模型中,然后设置“ thiskey”和“ otherkey”值,并添加“ isforeignkey = true”。

This is my final model for "usuario" : 这是我对“ usuario”的最终模型:

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; }
        }

    }

Thanks to @aminexplo for your help :) 感谢@aminexplo的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM