EF 6 infers the 0:1-n relationship using the navigation property by default convention. Relationship: Grade(0:1)-Student(many) Parent class: Grade Children class: Student
The Student.Grade property is reference navigation property of Grade class. So, there can be many students in a single grade. This will result in a one-to-many relationship between the Students and Grades table in the database, where the Students table includes foreign key Grade_GradeId . Notices:
The reference property Student.Grade is nullable, so it creates a nullable foreign key column Grade_GradeId in the Students table. This is called as optional one-to-many relationship, too.
These codes below falls into 0:1-m relation as above, too:
// Configure Student & StudentAddress entity
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address) // Mark Address property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
6. Convention 6 (1-1 relationship)
Relationship: Student(1)-StudentAddress(1)
Method: Using Fluent API where both ends are required
// Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasRequired(s => s.Address)
.WithRequiredPrincipal(ad => ad.Student);
//Set StudentName column size to 50modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(50);
//Setbyte[] type property as a concurrency columnmodelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.IsRowVersion();
8. EF Power Tools
Entity Framework 6 Power Tools are design-time utilities used in Visual Studio when working with the code-first development approach. It can create read-only entity data model.
Sets a modifier for a subsequent event emission that the event
data may be lost if the clients are not ready to receive messages (because of
network slowness or other issues, or because they’re connected through long
polling and is in the middle of a request-response cycle).
io.volatile.emit('an event', { some: 'data' }); // the clients may or may not receive it
Flag: ‘binary’
Specifies whether there is binary data in the emitted data.
Increases performance when specified. Can be true or false.
Emits an event to all connected clients. The following two are
equivalent:
const io = require('socket.io')();io.emit('an event sent to all connected clients'); // main namespaceconst chat = io.of('/chat');chat.emit('an event sent to all connected clients in chat namespace');
Note: acknowledgements are
not supported when emitting from namespace.
2.Rooms
Within each namespace, you can also define arbitrary channels
that sockets can join and leave.
Joining and leaving
You can call join to subscribe the
socket to a given channel:
And then simply use to or in (they are the same)
when broadcasting or emitting:
io.to('some room').emit('some event');
To leave a channel you call leave in the same fashion as join.
3.Socket
A Socket is the fundamental
class for interacting with browser clients. A Socket belongs to a certain Namespace (by default /) and uses an underlying Client to communicate.
socket.id
A unique
identifier for the session, that comes from the underlying Client.
socket.rooms
A hash of strings
identifying the rooms this client is in, indexed by room name.
socket.clientA reference to
the underlying Client object.
socket.emit(eventName[,
…args][, ack])
eventName(String)
args
ack(Function)
ReturnsSocket
Emits an event to the socket identified by the string name. Any
other parameters can be included. All serializable datastructures are
supported, including Buffer.
socket.on('news', (data) => {console.log(data);});// with several argumentssocket.on('news', (arg1, arg2, arg3) => {// ...});// or with acknowledgementsocket.on('news', (data, callback) => {callback(0);});
socket.join(room[,
callback])
room(String)
callback(Function)
ReturnsSocket for chaining
Adds the client to the room, and fires optionally a callback with err signature (if any).
io.on('connection', (socket) => {socket.join('room 237', () => {let rooms = Object.keys(socket.rooms);console.log(rooms); // [ <socket.id>, 'room 237' ]io.to('room 237').emit('a new user has joined the room'); // broadcast to everyone in the room});});
For your convenience, each socket automatically joins a
room identified by its id (see Socket#id). This makes it easy to
broadcast messages to other sockets:
io.on('connection', (socket) => {socket.on('say to someone', (id, msg) => {// send a private message to the socket with the given idsocket.to(id).emit('my message', msg);});});
socket.join(rooms[,
callback])
rooms(Array)
callback(Function)
ReturnsSocket for chaining
Adds the client to the list of room, and fires optionally a
callback with err signature (if any).
socket.leave(room[,
callback])
room(String)
callback(Function)
ReturnsSocket for chaining
Removes the client from room, and fires optionally a callback with err signature (if any).
Rooms are left
automatically upon disconnection.
socket.to(room) or
socket.in(room)
room(String)
ReturnsSocket for chaining
Sets a modifier for a subsequent event emission that the event
will only be broadcasted to
clients that have joined the given room (the socket itself
being excluded).
To emit to multiple rooms, you can call to several times.
io.on('connection', (socket) => {// to one roomsocket.to('others').emit('an event', { some: 'data' });// to multiple roomssocket.to('room1').to('room2').emit('hello');// a private message to another socketsocket.to(/* another socket id */).emit('hey');// WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room// named `socket.id` but the sender. Please use the classic `socket.emit()` instead.});
Note: acknowledgements are
not supported when broadcasting.
socket.compress(value)
value(Boolean) whether to following packet
will be compressed
ReturnsSocket for chaining
Sets a modifier for a subsequent event emission that the event
data will only be compressed if
the value is true. Defaults to true when you don’t call the method.
Sets a modifier for a subsequent event emission that the event
data will only be broadcast to
every sockets but the sender.
io.on('connection', (socket) => {socket.broadcast.emit('an event', { some: 'data' }); // everyone gets it but the sender});
Flag: ‘volatile’
Sets a modifier for a subsequent event emission that the event
data may be lost if the client is not ready to receive messages (because of
network slowness or other issues, or because they’re connected through long
polling and is in the middle of a request-response cycle).
io.on('connection', (socket) => {socket.volatile.emit('an event', { some: 'data' }); // the client may or may not receive it});
Flag: ‘binary’
Specifies whether there is binary data in the emitted data.
Increases performance when specified. Can be true or false.
var io = require('socket.io')();io.on('connection', function(socket){socket.binary(false).emit('an event', { some: 'data' }); // The data to send has no binary data});
Event:
‘disconnect’
reason(String) the reason of the
disconnection (either client or server-side)
const io = require('socket.io-client');// or with import syntaximport io from'socket.io-client';
io([url][,
options])
url(String) (defaults
to window.location)
options(Object)
forceNew(Boolean) whether to reuse an
existing connection
ReturnsSocket
Creates a new Manager for the given URL,
and attempts to reuse an existing Manager for subsequent
calls, unless the multiplex option is passed
with false. Passing this option is
the equivalent of passing 'force
new connection': true or forceNew:
true.
A new Socket instance is returned
for the namespace specified by the pathname in the URL, defaulting to /. For example, if the url is http://localhost/users, a transport connection
will be established to http://localhost and a Socket.IO
connection will be established to /users.
Query parameters can also be provided, either with the query option or directly in the url (example: http://localhost/users?token=abc).