Tuesday, October 20, 2015

power shell xmlNode Type prüfen

write-host (-Not ($parentNode.NodeType -eq "Comment"))

Tuesday, October 13, 2015

EF Code First mapping non public properties




in the Contextclass add:

protected override void OnModelCreating(DbModelBuilder modelBuilder)



{

base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Add(new NonPublicColumnAttributeConvention());



}

and this is the convention




/// Convention to support binding private or protected properties to EF columns.


///


public sealed class NonPublicColumnAttributeConvention : Convention






{

public NonPublicColumnAttributeConvention()



{

Types().Having(NonPublicProperties)

.Configure((config, properties) =>



{

foreach (PropertyInfo prop in properties)



{

config.Property(prop);



}

});

}

private IEnumerable<PropertyInfo> NonPublicProperties(Type type)



{

var matchingProperties = type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance)

.Where(propInfo => propInfo.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0)

.ToArray();

return matchingProperties.Length == 0 ? null : matchingProperties;



}

}




Thursday, October 08, 2015

Sql Server PIVOT Example

very good example from http://www.insidesql.org/blogs/cmu/sql_server/pivot-mit-2-wertefeldern

Set Nocount on
go
CREATE TABLE #p
( Name varchar(10),
ErfüllungsgradIst INTEGER,
ErfüllungsgradSoll INTEGER,
Maschine varchar(20) )
GO
INSERT INTO #p VALUES ('Maier', 3, 4, 'Drehbank')
INSERT INTO #p VALUES ('Maier', 2, 3, 'Fräsmaschine')
INSERT INTO #p VALUES ('Maier', 4, 4, 'Bohrmaschine')
INSERT INTO #p VALUES ('Huber', 1, 2, 'Drehbank')
INSERT INTO #p VALUES ('Huber', 2, 3, 'Fräsmaschine')
    
select *
from #p
      
Select Name, 'ErfüllungsgradSoll' as Erfüllungsgrad, coalesce(Drehbank, 0) as Drehbank,
coalesce(Fräsmaschine, 0) as Fräsmaschine, coalesce(Bohrmaschine, 0) as Bohrmaschine
from
(Select Name, ErfüllungsgradSoll , Maschine from #p) as SourceTable
PIVOT
(
min(ErfüllungsgradSoll)
FOR Maschine IN ( [Drehbank],[Fräsmaschine], [Bohrmaschine])
) as PivotTable
Union ALL
Select Name, 'ErfüllungsgradIst', coalesce(Drehbank,0), coalesce(Fräsmaschine,0), coalesce(Bohrmaschine, 0)
from
(Select Name, ErfüllungsgradIst, Maschine from #p) as SourceTable
PIVOT
(
min(ErfüllungsgradIst)
FOR Maschine IN ( [Drehbank],[Fräsmaschine], [Bohrmaschine])
) as PivotTable
Order By Name, Erfüllungsgrad
     

GO
drop Table #p

Wednesday, October 07, 2015

LINQ Query / Method Syntax Examples

Entity Framework


for Entity Framework: Load referenced Object first, then query Attribute of Object:
parent.Childs.Select(o=>o.ChildAttribute).FirstOrDefault(x => x.Name == name);


LINQ Query / Method Syntax Examples

Customer[] customers = Service.GetCustomers();
var query = from customer in customers
where customer.Name == "Hans"
from order in customer.Orders
where order.Quantity > 6
select new {order.OrderID, order.ProductID};

Customer[] customers = Service.GetCustomers();
var query = customers
.Where(c => c.Name == "Hans")
.SelectMany(c => c.Orders)
.Where(order => order.Quantity > 6)
.Select(order => new {order.OrderID, order.ProductID});

Entity Framework (EF) Code First Migrations: Seeding

you can use SQL(...) in Up/Down Methods, or Configuration.Seed or check if already exists

Entity Framework (EF) Code First Migration Update

If you want to update an existing database to current Version you have to set the EF Database Initializer:
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersionContext, Migrations.Configuration>());

when you first Access the db, EF checks the current Version and updates it if necesarry. You Need a parameterless contructor in your Context Class. Ist called several times when updating the DB. And you have to call the base constructor (of DBContext) and pass the correct connectionstringname if you want the correct database beiing updated, else the Default database is being updated (db Name=Namespace.classname of ypur EF Context)


Tuesday, October 06, 2015

Visual Studio: customize debugger display of classes

Shows Name, type if not null, if type null then Shows "Null"
e.g.:
Name = "Stückzahl", Type="Int"
Name = "Int", Type="Null"

class head:

[DebuggerDisplay("Name = {Name}, Type={null==Type?\"Null\":Type.Name}")]

public class DbAttribute



{




public Guid DbAttributeId { get; set; }

public string Name { get; set; }

public DbAttribute Type { get; set; }