I have two Models and a ViewModels for articles and posts from DB . One of them like this :
public class ArticleData
{
public string Id { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string Body { get; set; }
public string Section { get; set; }
public string Author { get; set; }
public string Avatar { get; set; }
public string BackgroundImage { get; set; }
public string Quote { get; set; }
public string QuoteAuthor { get; set; }
public string When { get; set; }
public string Followers { get; set; }
public string Likes { get; set; }
}
and i use BackgroundImage in CarouselView like this :
<CarouselView
HeightRequest="300"
Margin="0"
VerticalOptions="Center"
ItemsSource="{ Binding Images.Articles }">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<ffimageloading:CachedImage
Source="{ Binding BackgroundImage }"
VerticalOptions="Fill"
Aspect="AspectFill" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
And there is no problem with CarouselView and it shows images . But with another Model that likes this
public class FollowingsPostResponse
{
public List<FPostModel> data { get; set; }
public bool isSuccess { get; set; }
public int statusCode { get; set; }
public string message { get; set; }
}
public class FPostModel
{
public int id { get; set; }
public List<string> images { get; set; }
public string userName { get; set; }
public string userProfileImage { get; set; }
public string cityName { get; set; }
public int likesCounts { get; set; }
public string postCaption { get; set; }
}
and use like this
<CarouselView
HeightRequest="300"
Margin="0"
VerticalOptions="Center"
ItemsSource="{ Binding Home.Post }">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<ffimageloading:CachedImage
Source="{ Binding images }"
VerticalOptions="Fill"
Aspect="AspectFill" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
CarouselView does not show anythings . What's the problem ?
Thanks to #Jason and LeoZhu-MSFT
I solved my problem like this :
<CarouselView x:Name="MainCarouselView" ItemsSource="{Binding images}" Grid.Row="1" HeightRequest="150">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding .}"></Image>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
ItemsSource="{Binding images}" because CarouselView is in CollectionView with ItemsSource="{Binding Home.Post}"
I'm trying to learn asp.net core with razor and I'm trying to make a videogame database to keep a track of finished games, games I haven't played yet, etc.
But I have a problem. I have a table Game and a table Developer. Since a game can have many developers and a developer can have many games y made a third table DeveloperXGame.
They are something like this
public class Game
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Developer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DeveloperXGame
{
public int DeveloperId { get; set; }
public int JuegoId { get; set; }
public Developer Developer { get; set; }
public Game Game { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Game> Game { get; set; }
public DbSet<Developer> Developer { get; set; }
public DbSet<DeveloperXGame> DeveloperXGame { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DeveloperXGame>()
.HasKey(m => new { m.DeveloperId, m.GameId });
}
}
I already did the pages for the developers so I first create them manually. Now I'm trying to create the games and I want to show a select where I can select one or more developers of that list (next step will be to try to add them with ajax through the games page if they don't exists). But I'm lost beyond this point.
I don't know how to load the list of developers in that list and later on post how to save those selected items in the table DeveloperXGame.
Thanks
You can remove public DbSet<DeveloperXGame> DeveloperXGame { get; set; } from your context.
Index.cshtml.cs
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
public IndexModel(ApplicationDbContext context)
{
_context = context;
}
public Game Game { get; set; }
public IEnumerable<int> Developers { get; set; }
public IEnumerable<SelectListItem> DeveloperList { get; set; }
public IActionResult OnGet()
{
var developers = from m in _context.Developers
select m;
DeveloperList = developers.Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name });
return Page();
}
}
Here is the view Index.cshtml
#page
#model RazorPages.TestGame.Pages.Games.IndexModel
#{
ViewData["Title"] = "Index";
}
<div class="row">
<div class="col-md-4">
<form method="post">
<div class="form-group">
<label asp-for="Game.Name" class="control-label"></label>
<input asp-for="Game.Name" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Developers" class="control-label"></label>
<select asp-for="Developers" asp-items="Model.DeveloperList">
<option value="">All</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
You can also use ViewData to pass your developers list in your view. You can go through this sample web as in official doc here.Hope it helps you to get started!
We are using WebAPI 2.2, with OData v3 and BreezeJS and are having an issue when using inheritance, we have a setup along the following lines (simplified obviously for this issue)
We have a Vehicle abstract class and then two other classes (Bus and Car) which inherit from Vehicle, such as:
public abstract class Vehicle
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Bus : Vehicle
{
public int NumberOfSeats { get; set; }
}
public class Car : Vehicle
{
public string Colour { get; set; }
}
We then have an Activity class which can have a single Vehicle (either a Car or a Bus):
public class Activity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
public int VehicleId { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
We would like to be able to query the Activity and expand its Vehicle and get the relevent Car or Bus back, such as https://dummysitename/api/Activities?$expand=Vehicle, which is working fine. We would also like to be able to GET/PATCH/POST to endpoints for Cars and Buses (such as https://dummysitename/api/Cars and https://dummysitename/api/Buses), however we get a 404 when trying to do this.
Our metadata is created by the breeze EdmBuilder. We have tested it not using the EdmBuilder but using the ODataConventionModelBuilder and that works fine for these scenarios, but doesn't obviously work for us in the grander scheme of things as we will be utilizing breeze.js heavily.
Any ideas on why we can't use the Cars and Buses endpoints when using the EdmBuilder would be greatly appreciated.
UPDATE:
It would appear that the issue is being caused by the Vehicle being stipulated on the Activity class. With the Vehicle on the Activity class the EntityContainer section of the metadata looks like this:
<EntityContainer Name="TodoListContext" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<EntitySet Name="Activities" EntityType="ODataBreezejsSample.Models.Activity" />
<EntitySet Name="Vehicles" EntityType="ODataBreezejsSample.Models.Vehicle" />
<AssociationSet Name="Activity_Vehicle" Association="ODataBreezejsSample.Models.Activity_Vehicle">
<End Role="Activity_Vehicle_Source" EntitySet="Activities" />
<End Role="Activity_Vehicle_Target" EntitySet="Vehicles" />
</AssociationSet>
</EntityContainer>
however if the Vehicle is removed from the Activity class then that same section looks like this:
<EntityContainer Name="TodoListContext" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<EntitySet Name="Activities" EntityType="ODataBreezejsSample.Models.Activity" />
<EntitySet Name="Buses" EntityType="ODataBreezejsSample.Models.Bus" />
<EntitySet Name="Cars" EntityType="ODataBreezejsSample.Models.Car" />
</EntityContainer>
at which point the Bus and Car endpoints become available, however this is not really an option as we require the Activity to contain the base class Vehicle.
I have a ViewModel in a WPF Application with these two properties:
public Customer Customer { get; set; }
public ObservableCollection<Customer> Customers { get; set; }
Inside my view I Have a DXGrid. How do I bind the selected item to the customer property?
You should use SelectedRowsSource property. Bind it to ObservableCollection<Customer>. Your code will look like this:
public ObservableCollection<Customer> SelectedCustomers { get; set; }
public ObservableCollection<Customer> Customers { get; set; }
....
<dxg:GridControl ItemsSource="{Binding Customers}" AutoPopulateColumns="True">
<dxg:GridControl.View>
<dxg:TableView MultiSelectMode="Row" NavigationStyle="Row"
SelectedRowsSource="{Binding SelectedCustomers}" />
</dxg:GridControl.View>
</dxg:GridControl>
An existing DB schema has unique, non-primary, keys, and some foreign keys that rely on them.
Is it possible to define unique keys, which are not primary keys, in Entity Framework v4? How?
The Entity Framework 6.1 now supports uniques with both Data Annotations and Fluent API.
Data Annotations (Reference)
public class MyEntityClass
{
[Index(IsUnique = true)]
[MaxLength(255)] // for code-first implementations
public string MyUniqueProperty{ get; set; }
}
Fluent API (Reference)
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Entity<MyEntityClass>()
.Property(t => t.MyUniqueProperty)
.HasMaxLength(255) // for code-first implementations
.HasColumnAnnotation(
"Index",
new IndexAnnotation(new[]
{
new IndexAttribute("Index") { IsUnique = true }
})));
}
}
}
You have to apply an index and set the unique property to true. By default, indexes are non-unique according to documentation.
And also you have to install the Entity Framework 6.1 NuGet package in your project in order to use the new API for indexes.
Note about code-first implementations: A VARCHAR(MAX) cannot be part of a unique constraint. You must specify the maximum length either as a Data Annotation or in the Fluent API.
See also this MSDN blog post: http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx. In brief, this isn't supported in V4, though the EF team seems to have plans to support it in future releases.
I came across the same problem not long ago.
I was given a database with a few tables (see below).
public class ClinicDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Secretary> Secretarys { get; set; }
public DbSet<Disease> Diseases { get; set; }
public DbSet<Consultation> Consultations { get; set; }
public DbSet<Administrator> Administrators { get; set; }
}
The Users table was described like this:
public class User
{
[Key]
public Guid UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string IdentityCardNumber { get; set; }
public string PersonalNumericalCode { get; set; }
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
}
Next, I was asked to make sure that all the 'UserName' attributes would be unique. Since there is no annotation for that, I had to figure out a work-around. And here it is:
First, I changed my database context class to look like this:
public class ClinicDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Secretary> Secretarys { get; set; }
public DbSet<Disease> Diseases { get; set; }
public DbSet<Consultation> Consultations { get; set; }
public DbSet<Administrator> Administrators { get; set; }
public class Initializer : IDatabaseInitializer<ClinicDbContext>
{
public void InitializeDatabase(ClinicDbContext context)
{
if (!context.Database.Exists() || !context.Database.CompatibleWithModel(false))
{
if (context.Database.Exists())
{
context.Database.Delete();
}
context.Database.Create();
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Users_UserName ON dbo.Users ( UserName )");
}
}
}
}
The important part from above is the sql command which alters the table by enforcing a unique index on our desired column -> UserName in our case.
This method can be called from the main class for example:
class Program
{
static void Main(string[] args)
{
Database.SetInitializer<ClinicDbContext>(new ClinicDbContext.Initializer());
using (var ctx = new ClinicDbContext())
{
Console.WriteLine("{0} products exist in the database.", ctx.Users.Count());
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
The final issue, which occurred when trying to run the the Program class was the following: column in table is of a type that is invalid for use as a key column in an index
To solve this issue, I just added a [MaxLength(250)] annotation for the UserName attribute.
Here is how the User class looks in the end:
public class User
{
[Key]
public Guid UserId { get; set; }
[MaxLength(250)]
public string UserName { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string IdentityCardNumber { get; set; }
public string PersonalNumericalCode { get; set; }
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
}
Hope it will solve your problem too!
I've tried defining the following tables:
Orders [Id (primary, identity), ClientName, FriendlyOrderNum (unique)]
OrderItems [Id (primary, identity), FriendlyOrderNum (unique), ItemName]
And a foreign key mapping from OrderItems.FriendlyOrderNum (Mant) to Orders.FriendlyOrderNum (one).
If unique non-primary keys are possible the following SSDL should work:
<Schema Namespace="EfUkFk_DbModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="EfUkFk_DbModelStoreContainer">
<EntitySet Name="OrderItems" EntityType="EfUkFk_DbModel.Store.OrderItems" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Orders" EntityType="EfUkFk_DbModel.Store.Orders" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="OrderItems">
<Key>
<PropertyRef Name="RowId" />
</Key>
<Property Name="RowId" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="OrderNum" Type="char" Nullable="false" MaxLength="5" />
<Property Name="ItemName" Type="varchar" MaxLength="100" />
</EntityType>
<!--Errors Found During Generation:
warning 6035: The relationship 'FK_OrderItems_Orders' has columns that are not part of the key of the table on the primary side of the relationship. The relationship was excluded.
-->
<EntityType Name="Orders">
<Key>
<PropertyRef Name="RowId" />
</Key>
<Property Name="RowId" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ClientName" Type="varchar" MaxLength="100" />
<Property Name="OrderNum" Type="char" Nullable="false" MaxLength="5" />
</EntityType>
<!-- AsafR -->
<Association Name="FK_OrderItems_Orders">
<End Role="Orders" Type="EfUkFk_DbModel.Store.Orders" Multiplicity="1">
</End>
<End Role="OrderItems" Type="EfUkFk_DbModel.Store.OrderItems" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Orders">
<PropertyRef Name="OrderNum" />
</Principal>
<Dependent Role="OrderItems">
<PropertyRef Name="OrderNum" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema></edmx:StorageModels>
It doesn't. There's also no possibility for adding more <key> elements in an <EntityType>.
My conclusion is that non-primary unique keys are not support in EF 4.
You can use DataAnnotations validation as well.
I've created this (UniqueAttribute) class, that inherits ValidationAttribute, and when applied to a property, the values of that column will be retrieved and validated against, during validation.
You can grab the raw code from here.