How to map properties in EF CTP5 - entity-framework-4

In CTP 4 we could choose the properties we want to map like so:
this.MapSingleType(i => new
{
i.Id,
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
How do we achieve this in CTP5?
I tried using the following Map configuration but this does not appear to work since I still have to explicitly ignore (this.Ignore(..)) the properties I do not wish to map:
Map(config =>
{
config.Properties(i => new
{
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
config.ToTable("Images");
});
Considering the new API is supposed to be more fluent, it's strange that I have to write more code to achieve the same thing.
Thanks
Ben

This blog post has ctp 5 mapping samples.
http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx
Make a clr-nullable property required:
modelBuilder.Entity<Product>()
.Property(p => p.Name)
.IsRequired();
Change string length:
modelBuilder.Entity<Product>()
.Property(p => p.Name)
.HasMaxLength(50);
Switch off Identity:
modelBuilder.Entity<Product>()
.Property(p => p.ProductId)
.HasDatabaseGenerationOption(DatabaseGenerationOption.None);
Ignore a property:
modelBuilder.Entity<Person>()
.Ignore(p => p.Name);
Table & Column Mapping
Change column name:
modelBuilder.Entity<Category>()
.Property(c => c.Name)
.HasColumnName("cat_name");
Change table name:
modelBuilder.Entity<Category>()
.ToTable("MyCategories");
Change table name with schema:
modelBuilder.Entity<Category>()
.ToTable("MyCategories", "sales");

CTP5 is indeed more powerful and flexible both in Data Annotations and fluent API. For example in CTP4 if we wanted to exclude a property from mapping we would have to explicitly map everything else with MapSingleType in order to skip the one we don't want, like the way you mentioned.
In CTP5 this can be done simply by using [NotMapped] attribute on the property or by this fluent API code:
this.Ignore(i => i.Id);
And you are done, no need to invoke Map method.

Related

Symfony 4 + Select 2 - add more options to entityType

I'm working on app based on Symfony 4 with Select2 library.
In my src/Form/PostType.php file I declared field tag, where user should be able to set one of predeclared Tag or add new one (by type tag name and press enter).
$builder
->add('tags', EntityType::class, [
'class' => Tag::class,
'choice_label' => 'name',
'mapped' => false,
'expanded' => false,
'multiple' => true,
'required' => false,
]);
From the frontend side I'm using select2 library to handle with displaying tags field.
In below example fist tag was chosen from the existed entity in database, the second one should be saved in this second.
Any idea what should I changed into filed declaration to make this field valid also for new tags?
Controller is ready, only issue is to pass form validation :)
EDIT:
Relations in ORM looks like this:
class Company {
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Tag", mappedBy="companies")
*/
private $tags;
}
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Company", inversedBy="tags")
*/
private $companies;
}
and there is no other validation than in code above
You have set the field to be mapped = false. If a field is unmapped you have to handle form validation manually. Can you share your Entities code , any validation code if it's written?

Kendo MVC Grid Default Sort by Multiple Column Fails

#(Html.Kendo().Grid<...>()
.Columns(columns =>
{
columns.Bound(j => j.Type);
columns.Bound(j => j.Code);
})
.Sortable(s => s.Enabled(true))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Sort(p => { p.Add("Code").Ascending(); p.Add("Type").Ascending(); })
.Model(model => model.Id(j => j.ID))
.Read(...)
.ServerOperation(true)
)
)
I have a Kendo MVC Grid as above and want to sort it first by type, then by code.
I have found such implementation on official Telerik forum: Default Grid Sorting
However it seems it fails to work...
The records are sorted by two columns, and apparently it is sorted by Type, but it fails to sort by Code afterwards...As Default should after CG...
What am I missing and how can I fix the problem?
For anyone else looking for a solution, using the model property names worked for me, i.e.
.Sort(s =>
{
s.Add("Code").Ascending();
s.Add("Type").Ascending();
})
Try this way
.Sort(p=> {p.Add(s=>s.Code).Ascending(); p.Add(s=>s.Type).Ascending();})

Infragistics hidden column update

I'm using Infragistics on an Entity Framework code-first MVC project. I want to display a table with a hidden column (the ID) and it has to be editable. Here is what I've got so far:
<table id="igTests"></table>
#(Html.Infragistics().Grid<BusinessModel.VO.TestVO>().ID("igTests")
.AutoGenerateColumns(false)
.Columns(column =>
{
column.For(x => x.TestId).DataType("int").HeaderText("id");
column.For(x => x.TestNum).DataType("int").HeaderText("Test num");
column.For(x => x.Type).DataType("string").HeaderText("Type");
column.For(x => x.Nature).DataType("string").HeaderText("Nature");
column.For(x => x.TeamName).DataType("string").HeaderText("Team");
column.For(x => x.CreateDate).DataType("date").HeaderText("Creation date");
})
.Features(feature => {
feature.Sorting().CaseSensitive(true);
feature.Filtering().Mode(FilterMode.Simple);
})
.PrimaryKey("TestId")
.DataSource(Model.TestsVO.AsQueryable())
.DataBind()
.Render())
This is what is displayed:
Now lets add the update feature (i know the readOnly is useless since we are not supposed to see it):
feature.Updating().EnableAddRow(true).EnableDeleteRow(true).EditMode(GridEditMode.Row).ColumnSettings(settings =>
settings.ColumnSetting().ColumnKey("TestId").ReadOnly(true)
);
And hide my ID-column:
column.For(x => x.TestId).DataType("int").HeaderText("id").Hidden(true);
Here is what i get. As you can see the table acts like my ID-column was visible.
This happened when I added the update feature. Do you have any idea on how I could fix the "Add new row" row acting like my column was visible? Thanks in advance.
Probably you should add this
}).Features(features => features.Hiding()
.ColumnSettings(settings =>
{
settings.ColumnSetting().ColumnKey("id").Hidden(true).AllowHiding(false)
http://www.infragistics.com/products/jquery/sample/grid/column-hiding-on-initialization
I got it to work using .Width("0px") and ReadOnly on my ID-column. A bit dirty but had no other choice...

Telerik MVC Grid Delete issue, Delete doesn't pass string key value to controller

I can't tell if the following issue is my code or a Telerik bug. Thanks for any help.
My Telerik .cshtml code looks like the following. Note that s.ID is a string not an integer.
...
.ToolBar(commands => commands.Insert())
.DataKeys(keys => keys.Add(s => s.ID))
.DataBinding(dataBinding =>
{
dataBinding.Server()
.Select("Edit", "DataImport")
.Insert("Insert", "DataImport")
.Update("Save", "DataImport")
.Delete("Delete", "DataImport");
})
.Columns(columns =>
...
On the controller side:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(string key) // Here, the key comes in as null
However, with the Save method it works. "key" in the following is correctly set to the string key value of the row.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(string key, Model model) // Here, the key is correct
In both cases the URL looks right, e.g. .../Delete/keyHere
I agree with CD Smith double check your data types. If you want to pass your "ID" field to the controller under the variable name "Key". You need to use the RouteKey() method.
.DataKeys(keys => keys.Add(s => s.ID).RouteKey("Key"))

Circular Reference- LINQ to SQL

HI
I am using telerik mvc grid with ajax binding
<%Html.Telerik().Grid<UserManagement.Models.setupEmployee>()
.Name("setupEmployees")
.DataBinding(dataBinding => dataBinding
//Ajax binding
.Ajax()
//The action method which will return JSON
.Select("_AjaxBindingEmployee", "UM")
).
Columns(colums =>
{
colums.Bound(o => o.EmployeeName).Title("Name");
colums.Bound(o => o.setupDesignation.Title).Title("Designation");
colums.Bound(o => o.Gender);
colums.Bound(o => o.DOB);
colums.Bound(o => o.EmployeeID).Format(
%><%Html.ActionLink("Edit", "Edit", new { Id = "{0}" }).ToString()).Encoded(false);
})
.Pageable()
.Sortable()
.Filterable()
.PrefixUrlParameters(false)
.Render();
%>
when i try to populate the grid with
return db.setupEmployees
i get the following error
A circular reference was detected while serializing an object of type. As i have relationship of this table with other tables.
To avoid this i may have two options either i use viewmodel or disable the relationships which is not possible. Any other sugession from your side
Regards
The associations created in the LinqToSql designer are under your control.
You can remove them (does not change the database).
You can edit them so they generate single sided properties instead of dual facing properties (does not change the database).
You can edit them so they generate no properties at all (does not change database).
The designer's file is a mapping file, it does not change the database.

Resources