Model created by StoreFor<> and multi HasManayAssociation Attribute
I have a problem with HasManyAssociation Attribute.
My sample has 7 classes, having associations with each other.
School HasMany ClassRoom and Club
ClassRoom HasMany Lecture and Facility
Club HasMany Event and ClubLecture
It's part of my sample...
-- School Class
This class has many ClassRooms and Clubs classes
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[HasManyAssociation(Model = "Test.school.ClassRoom", Name = "classRooms", PrimaryKey = "Id", AssociationKey = "ClassRooms", ForeignKey = "SchoolId")]
[HasManyAssociation(Model = "Test.school.Club", Name = "clubs", PrimaryKey = "Id", AssociationKey = "Clubs", ForeignKey = "SchoolId")]
[Model(Name = "Test.school.School")]
public partial class School
{
public School()
{
this.ClassRooms = new HashSet<ClassRoom>();
this.Clubs = new HashSet<Club>();
}
[ModelField(IDProperty = true)]
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
[ModelField(Ignore = true)]
public virtual ICollection<ClassRoom> ClassRooms { get; set; }
public virtual ICollection<Club> Clubs { get; set; }
}
}
-- ClassRoom Class
This Class belongs to School class
and has many Lectures and Facilities classes
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[HasManyAssociation(Model = "Test.school.Lecture", Name = "lectures", PrimaryKey = "Id", AssociationKey = "Lectures", ForeignKey = "ClassRoomId")]
[HasManyAssociation(Model = "Test.school.Facility", Name = "facilities", PrimaryKey = "Id", AssociationKey = "Facilities", ForeignKey = "ClassRoomId")]
[BelongsToAssociation(Model = "Test.school.School")]
[Model(Name = "Test.school.ClassRoom")]
public partial class ClassRoom
{
public ClassRoom()
{
}
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int SchoolId { get; set; }
public string Name { get; set; }
public string Location { get; set; }
[ModelField(Ignore = true)]
public virtual School School { get; set; }
}
}
-- Club Class
This class belongs to School class
and has many Events and ClubLectures
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[HasManyAssociation(Model = "Test.school.ClubLecture", Name = "clubLectures", PrimaryKey = "Id", AssociationKey = "ClubLectures", ForeignKey = "ClubId"),
HasManyAssociation(Model = "Test.school.Event", Name = "events", PrimaryKey = "Id", AssociationKey = "Events", ForeignKey = "ClubId")]
[BelongsToAssociation(Model = "Test.school.School")]
[Model(Name = "Test.school.Club")]
public partial class Club
{
public Club()
{
this.ClubLectures = new HashSet<ClubLecture>();
this.Events = new HashSet<Event>();
}
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int SchoolId { get; set; }
public string Name { get; set; }
[ModelField(Ignore = true)]
public virtual School School { get; set; }
[ModelField(Ignore = true)]
public virtual ICollection<ClubLecture> ClubLectures { get; set; }
[ModelField(Ignore = true)]
public virtual ICollection<Event> Events { get; set; }
}
}
-- Facility Class
This class belongs to ClassRoom class
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[BelongsToAssociation(Model = "Test.school.ClassRoom")]
[Model(Name = "Test.school.Facility")]
public partial class Facility
{
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int ClassRoomId { get; set; }
public string Name { get; set; }
public string Purpose { get; set; }
[ModelField(Ignore = true)]
public virtual ClassRoom ClassRoom { get; set; }
}
}
-- Lecture Class
This class belongs to ClassRoom class
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[BelongsToAssociation(Model = "Test.school.ClassRoom")]
[Model(Name = "Test.school.Lecture")]
public partial class Lecture
{
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int ClassRoomId { get; set; }
public string Name { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
[ModelField(Ignore = true)]
public virtual ClassRoom ClassRoom { get; set; }
}
}
-- Event Class
This class belongs to Club class
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[BelongsToAssociation(Model = "Test.school.Club")]
[Model(Name = "Test.school.Event")]
public partial class Event
{
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int ClubId { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public string Description { get; set; }
[ModelField(Ignore = true)]
public virtual Club Club { get; set; }
}
}
-- ClubLecture Class
This class belongs to Club class
Code:
namespace Test.Models
{
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
[BelongsToAssociation(Model = "Test.school.Club")]
[Model(Name = "Test.school.ClubLecture")]
public partial class ClubLecture
{
[ModelField(IDProperty = true)]
public int Id { get; set; }
public int ClubId { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public Nullable<int> MaxMember { get; set; }
[ModelField(Ignore = true)]
public virtual Club Club { get; set; }
}
}
-- part of cshtml
I create client model with StoreFor<>
Code:
Html.X().GridPanel()
.ID("gvSchool")
.AutoDestroy(true)
.Width(500)
.Height(50)
.Hidden(false)
.Store(
Html.X().StoreFor<Test.Models.School>()
.ID("storeSchool")
.AutoDestroy(false)
.Data(Model)
)
-- Created Javascript
only one association defined
Code:
items: [{
store: {
model: Ext.define("Test.school.School", {
extend: "Ext.data.Model",
ns: "School",
fields: [{
name: "Id",
type: "int"
}, {
name: "Name",
type: "string"
}, {
name: "Location",
type: "string"
}, {
name: "Capacity",
type: "int"
}, {
name: "Area",
type: "float"
}, {
name: "Clubs"
}],
idProperty: "Id",
associations: [{
type: "hasMany",
associationKey: "ClassRooms",
primaryKey: "Id",
model: "Test.school.ClassRoom",
foreignKey: "SchoolId",
name: "classRooms"
}]
}),
storeId: "storeSchool",
ns: "School",
autoLoad: true,
data: {
"ClassRooms": [{
"Facilities": [],
"Lectures": [],
"Id": 1,
"SchoolId": 1,
"Name": "ClassRoom 1",
"Location": "1st floor - 1",
"Capacity": 50
}, {
"Facilities": [],
"Lectures": [],
"Id": 2,
"SchoolId": 1,
"Name": "ClassRoom 2",
"Location": "1st floor - 2",
"Capacity": 50
}],
"Clubs": [],
"Id": 1,
"Name": "School 1",
"Location": "Seoul",
"Capacity": 100,
"Area": 200.00
},
proxy: {
type: 'memory'
}
},
id: "gvSchool",
ns: "School",
height: 50,
width: 500,
xtype: "grid",
columns: {
ns: "School",
items: [{
ns: "School",
xtype: "rownumberer",
align: "center"
}, {
ns: "School",
width: 70,
align: "center",
dataIndex: "Id",
text: "School Id"
}]
}, ......
-- part of Expected Javascript
I expected two associations like below
Code:
associations: [{
type: "hasMany",
associationKey: "ClassRooms",
primaryKey: "Id",
model: "Test.school.ClassRoom",
foreignKey: "SchoolId",
name: "classRooms"
}, {
type: "hasMany",
associationKey: "Club",
primaryKey: "Id",
model: "Test.school.Club",
foreignKey: "SchoolId",
name: "clubs"
}]
From definition AbstractAssociationAttribute in AbstractAssociationAttribute.cs file
AllowMultiple = true
HasManyAssociationAttribute inherit AbstractAssociationAttribute...
Anything I missed?
Thanks in advance ^^.
Sorry ^^ I wraped code with CODE tags
Quote:
Originally Posted by
Baidaly
It's my first time to write at forum.
Be nice to newbie, please ^^.
explicitly define this associations in the Model? How??
Quote:
Originally Posted by
Baidaly
Sorry for the delay.
I'm not sure that it's possible to use the HasManyAssociation attribute more than once. I'll ask my colleagues about it.
However, you always should be able explicitly define this associations in the Model. Did you try it? Did it work for you?
"explicitly define this associations in the Model" => can you show me how, please? T_T