2010-11-12 45 views
1

我无法让我的应用程序插入到具有外键关系的表中。 我正在使用SqLite3 & Mono 2.8。单声道是否支持正确处理System.Data.Linq.Mapping.AssociationAttribute

我的sqlite的脚本如下所示:

CREATE TABLE IF NOT EXISTS appointments (
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    name VARCHAR(100) UNIQUE); 

CREATE TABLE IF NOT EXISTS courses_appointments (
    course_code VARCHAR(50) NOT NULL REFERENCES courses (code), 
    appointment_id INTEGER NOT NULL REFERENCES appointments (id)); 

CREATE TABLE IF NOT EXISTS courses (
    code VARCHAR(50) NOT NULL PRIMARY KEY, 
    name VARCHAR(255) NOT NULL UNIQUE); 

所以我有很多一对多的关系; 接着,我有映射类:

[Table(Name="appointments")] 
public partial class Appointment 
{ 
    private EntitySet<CourseAppointment> _courseAppointments; 

    public Appointment() 
    { 
     _courseAppointments = new EntitySet<CourseAppointment>(); 
    } 

    [Column(Name="id", AutoSync=AutoSync.OnInsert, IsPrimaryKey=true, IsDbGenerated=true)] 
    public int Id { get; set; } 

    [Column(Name="name", CanBeNull=false)] 
    public string Name { get; set; } 

    [Association(Storage="_courseAppointments", Name="appointments__courses_appointments", ThisKey="Id", OtherKey="AppointmentId")] 

    public EntitySet<CourseAppointment> CourseAppointments 
    { 
     get { return _courseAppointments; } 
     set { _courseAppointments.Assign(value); } 
    } 
} 

[Table(Name="courses_appointments")] 
public partial class CourseAppointment 
{ 
    private string _courseCode; 
    private int _appointmentId; 

    private EntityRef<Appointment> _appointment; 
    private EntityRef<Course> _course; 

    [Column(Name="_course_code", Storage="_courseCode", CanBeNull=false)] 
    public string CourseCode 
    { 
     get 
     { 
     return _courseCode; 
     } 
     set 
     { 
     if (_course.HasLoadedOrAssignedValue) 
      throw new ForeignKeyReferenceAlreadyHasValueException(); 

     this._courseCode = value;  
     } 
} 

    [Column(Name="appointment_id", Storage="_appointmentId")] 
    public int AppointmentId 
    { 
     get 
     { 
     return _appointmentId; 
     } 
     set 
     { 
     if (_appointment.HasLoadedOrAssignedValue) 
      throw new ForeignKeyReferenceAlreadyHasValueException(); 

    _appointmentId = value;  
     } 
    } 

    [Association(Name="appointments__courses_appointments", Storage="_appointment", ThisKey="AppointmentId", OtherKey="Id", IsForeignKey=true, DeleteOnNull=true)] 
    public Appointment Appointment 
    { 
     get 
     { 
     return _appointment.Entity; 
     } 
     set 
     { 
     var previousValue = _appointment.Entity; 

    if (previousValue == value && _appointment.HasLoadedOrAssignedValue) 
      return; 

     if (previousValue != null) 
     { 
      _appointment.Entity = null; 
    previousValue.CourseAppointments.Remove(this); 
    } 

     _appointment.Entity = value; 

    if (value != null) 
    { 
    value.CourseAppointments.Add(this); 
    _appointmentId = value.Id; 
    } 
     else 
    { 
    _appointmentId = 0; 
    }  
     } 
    } 

    [Association(Name="courses__courses_appointments", Storage="_course", ThisKey="CourseCode", OtherKey="Code", IsForeignKey=true, DeleteOnNull=true)] 
    public Course Course 
    { 
     get 
     { 
     return _course.Entity; 
     } 
     set 
     { 
     var previousValue = _course.Entity; 

     if (previousValue == value && _course.HasLoadedOrAssignedValue) 
    return; 

    if (previousValue != null) 
    { 
      _course.Entity = null; 
    previousValue.CourseAppointments.Remove(this); 
    } 

    _course.Entity = value; 

    if (value != null) 
     { 
      value.CourseAppointments.Add(this); 
    _courseCode = value.Code; 
    } 
     else 
    { 
    _courseCode = null; 
    }  
     } 
    } 
} 

[Table(Name="courses")] 
public partial class Course 
{  
    private EntitySet<CourseAppointment> _courseAppointments; 

    public Course() 
    { 
     _courseAppointments = new EntitySet<CourseAppointment>(); 
    } 

    [Column(Name="code", CanBeNull=false, IsPrimaryKey=true)] 
    public string Code { get; set; } 

    [Column(Name="name", CanBeNull=false)] 
    public string Name { get; set; } 

    [Association(Name="courses__courses_appointments", Storage="_courseAppointments", ThisKey="Code", OtherKey="CourseCode")] 
    public EntitySet<CourseAppointment> CourseAppointments 
    { 
     get { return _courseAppointments; } 
     set { _courseAppointments.Assign(value); } 
    } 
} 
} 

在i执行的代码:

var a =new Appointment() { Name = "abracadabra" }; 
appointments.InsertAllOnSubmit(entities); 
dc.SubmitChanges(); 

,则抛出异常:

System.ArgumentException: Value does not fall within the expected range. at DbLinq.Util.MemberInfoExtensions.GetMemberValue (System.Reflection.MemberInfo memberInfo, System.Object o) [0x00000] in :0 at System.Data.Linq.DataContext+c__AnonStorey2C.<>m__29 (System.Data.Linq.Mapping.MetaDataMember m) [0x00000] in :0 at System.Linq.Enumerable+c__Iterator10`2[System.Data.Linq.Mapping.MetaDataMember,System.Object].MoveNext() [0x00000] in :0 at System.Collections.Generic.List`1[System.Object].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in :0 at System.Collections.Generic.List`1[System.Object]..ctor (IEnumerable`1 collection) [0x00000] in :0 at System.Linq.Enumerable.ToList[Object] (IEnumerable`1 source) [0x00000] in :0 at System.Data.Linq.DataContext.UpdateReferencedObjects (System.Object root) [0x00000] in :0 at System.Data.Linq.DataContext.InsertEntity (System.Object entity, DbLinq.Data.Linq.Sugar.QueryContext queryContext) [0x00000] in :0 at System.Data.Linq.DataContext.SubmitChangesImpl (ConflictMode failureMode) [0x00000] in :0 at System.Data.Linq.DataContext.SubmitChanges (ConflictMode failureMode) [0x00000] in :0 at System.Data.Linq.DataContext.SubmitChanges() [0x00000] in :0 at Controllers.HomeController.InsertAppointments (System.Data.Linq.DataContext dc) [0x00000] in :0 at Controllers.HomeController.Index() [0x00000] in :0 

DataContext.Log包含此:

INSERT INTO "appointments" ("name") VALUES (:Name) -- :Name: Input String (Size = 0; Prec = 0; Scale = 0) [слесарь по обслуживанию буровых 4-5 разряда] -- Context: SQLite Model: AttributedMetaModel Build: 3.5.0.0 SELECT last_insert_rowid() -- Context: SQLite Model: AttributedMetaModel Build: 3.5.0.0 

当我评论AssociationAttribute on Appointment.CourseAppointments一切正常...

回答

0

是的,它的确如此!我已经标记CourseAppointment.CourseCode和CourseAppointment.AppointmentId分布式主键与

IsPrimaryKey =真

相关问题